c++ - If/else 循环:C++ 程序:不会显示最终提示/最终循环
问题描述
我的第一门 C++ 课程的这项作业遇到了很多麻烦。
我已经想出了如何让它正确地询问用户他们想要使用什么操作(加、减、乘),生成 0-9 之间的随机数,以及如何让用户解决问题并在出现问题时做出响应是正确还是不正确。
在此之后,程序应该询问用户是要继续(按 y)还是退出(按 Q),当用户输入任何其他字母时,会向用户显示错误消息,但由于某种原因,这运行程序时部分不显示。
如何让循环正常工作,让我做最后的提示,然后只在按 Y 时重复整个程序或按 Q 时退出?
注意:总的来说,我对编码非常陌生,这是我的第一门 C++ 课程,所以我还不知道如何使这段代码更简洁:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
while (true)
{
// Generate two random single-digit integers btwn 0-9
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
int operation, play, num3, guess, Y, Q;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
}
else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
while (guess != num3)
{
int play, Y, Q;
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cin >> play;
if (play != Y || play != Q)
{
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cin >> play;
}
else
{
if (play == Y)
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else if (play == Q)
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
break;
}
}
}
return 0;
}
解决方案
这里的东西很少...
1. 只播一次
移出srand(time(0));
while 循环并移到main
. 如果您在同一秒内重复播种(如果time(0)
没有改变),您将获得两次相同的“随机”数字。
2.num3
如果他们没有输入有效的operation
怎么办?
你永远不会 initalize num3
,所以如果他们不选择一个 valid operation
,num3
就会有一个垃圾值。然后你继续运行一个循环,其条件取决于num3
's 的值!( while (guess != num3)
)
3.else { ... if {
一样else if {
在您的最后一个循环中,将if (play == Y)
andelse if (play == Q)
带出嵌套if
并使其成为else if
.
4.你的最后一个循环条件不正确
真的while (guess != num3)
对吗?您想循环直到他们输入有效输入,那么为什么要循环 while guess != num3
?
推荐阅读
- typescript - 类方法而不是箭头函数
- javascript - 使用 setTimeout() 和 setInterval() 的类方法在重复调用时意外工作
- android - 如何在 Android-Kotlin 中没有构造函数值的情况下创建存储库对象?
- reactjs - 如何将预制静态页面添加到您的 gatsby 项目中?
- json - JWT 刷新令牌使用
- javascript - 如何在multer中检查文件类型
- python - 如何在 python 上使用 selenium 检查是否已单击按钮
- firebase - Firebase 使用 GMAIL 触发电子邮件和 SMTP 连接 URI
- android - 运行 Flutter 项目的 android Studio 出现问题
- python - 用 Pandas 从 Multindex 表中绘制特定列