If/else循环:C++;项目:韩元';t显示最终提示/最终循环 在我的第一个C++课程中,我的作业有很多问题。
我已经找到了如何让它正确地询问用户他们想要使用什么操作(加、减、乘),生成0-9之间的随机数,以及如何要求用户解决问题,并在问题正确或不正确时作出响应 在此之后,程序应询问用户是否要继续(按y键)或退出(按Q键),并在用户输入任何其他字母时向用户显示错误消息,但由于某些原因,此部分在运行程序时不会显示 如何使循环正常工作,允许我执行最终提示,然后仅在按Y键时重复整个程序,或在按Q键时退出 注意:我对编码很陌生,这是我的第一个C++课程,所以我还不知道如何使代码更简洁:If/else循环:C++;项目:韩元';t显示最终提示/最终循环 在我的第一个C++课程中,我的作业有很多问题。,c++,loops,debugging,if-statement,while-loop,C++,Loops,Debugging,If Statement,While Loop,我已经找到了如何让它正确地询问用户他们想要使用什么操作(加、减、乘),生成0-9之间的随机数,以及如何要求用户解决问题,并在问题正确或不正确时作出响应 在此之后,程序应询问用户是否要继续(按y键)或退出(按Q键),并在用户输入任何其他字母时向用户显示错误消息,但由于某些原因,此部分在运行程序时不会显示 如何使循环正常工作,允许我执行最终提示,然后仅在按Y键时重复整个程序,或在按Q键时退出 注意:我对编码很陌生,这是我的第一个C++课程,所以我还不知道如何使代码更简洁: #包括 #包括 #包括 #
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
while(true)
{
//生成两个随机单位数整数btwn 0-9
srand(时间(0));
int num1=rand()%10;
int num2=rand()%10;
整数运算,play,num3,guess,Y,Q;
//如果num1 这里几乎没有什么东西
1.只播一次种子
将srand(时间(0));
移出while循环并移动到main
的顶部。如果在同一秒钟内重复播种(如果time(0)
没有改变),您将两次获得相同的“随机”数
2.num3
如果没有输入有效的操作
,会发生什么情况?
您从不初始化num3
,因此,如果他们没有选择有效的操作
,num3
将有一个垃圾值。然后您继续运行一个循环,其条件取决于num3
的值!(while(guess!=num3)
)
3.else{…if{
与else if{
在最后一个循环中,将if(play==Y)
和else if(play==Q)
从嵌套的if
中取出,并使它们成为else if
4.上次循环条件不正确
while(guess!=num3)
真的正确吗?你想循环直到他们输入有效的输入,那么为什么你在guess!=num3
时循环?这里没有什么东西
1.只播一次种子
将srand(时间(0));
移出while循环并移动到main
的顶部。如果在同一秒钟内重复播种(如果time(0)
没有改变),您将两次获得相同的“随机”数
2.num3
如果没有输入有效的操作
,会发生什么情况?
您从不初始化num3
,因此,如果他们没有选择有效的操作
,num3
将有一个垃圾值。然后您继续运行一个循环,其条件取决于num3
的值!(while(guess!=num3)
)
3.else{…if{
与else if{
在最后一个循环中,将if(play==Y)
和else if(play==Q)
从嵌套的if
中取出,并使它们成为else if
4.上次循环条件不正确
while(guess!=num3)
真的正确吗?你想循环直到他们输入有效的输入,那么为什么你在guess!=num3
时循环?最好使用开关盒来选择正确的操作,如下所示:
Switch(operation) {case 1: break;}
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess,num1,num2,temp;
srand(time(0));
char play;
do
{
// Generate two random single-digit integers btwn 0-9
num1 = rand() % 10;
num2 = rand() % 10;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
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;
}
}while(operation>3 || operation<1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
}
do
{
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;
}
}while(play!='Y' && play!='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}while(play=='Y');
return 0;
}
您还需要再添加一个
因此,正确的代码应该如下所示:
Switch(operation) {case 1: break;}
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess,num1,num2,temp;
srand(time(0));
char play;
do
{
// Generate two random single-digit integers btwn 0-9
num1 = rand() % 10;
num2 = rand() % 10;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
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;
}
}while(operation>3 || operation<1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
}
do
{
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;
}
}while(play!='Y' && play!='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}while(play=='Y');
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数运算,num3,猜测,num1,num2,温度;
srand(时间(0));
角色扮演;
做
{
//生成两个随机单位数整数btwn 0-9
num1=rand()%10;
num2=rand()%10;
//如果num1 cout最好使用开关盒来选择正确的操作,如下所示:
Switch(operation) {case 1: break;}
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess,num1,num2,temp;
srand(time(0));
char play;
do
{
// Generate two random single-digit integers btwn 0-9
num1 = rand() % 10;
num2 = rand() % 10;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
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;
}
}while(operation>3 || operation<1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
}
do
{
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;
}
}while(play!='Y' && play!='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}while(play=='Y');
return 0;
}
您还需要再添加一个
因此,正确的代码应该如下所示:
Switch(operation) {case 1: break;}
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess,num1,num2,temp;
srand(time(0));
char play;
do
{
// Generate two random single-digit integers btwn 0-9
num1 = rand() % 10;
num2 = rand() % 10;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
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;
}
}while(operation>3 || operation<1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
}while(guess!=num3);
cout << "That is correct!" << endl;
cout << "" << endl;
break;
}
do
{
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;
}
}while(play!='Y' && play!='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}while(play=='Y');
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数运算,num3,猜测,num1,num2,温度;
srand(时间(0));
角色扮演;
做
{
//生成两个随机单位数整数btwn 0-9
num1=rand()%10;
num2=rand()%10;
//如果num1 cout在第二个while循环中发现了问题。play
变量应该声明为char
而不是int
。另外,您不需要将其与Y
和Q
整数变量进行比较。下面是一个解决方案。我希望它能帮助您:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
bool loop = true;
while (loop)
{
// 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.\n\t-----------------------" << endl;
cout << "\tEnter 1 to add,\n\tEnter 2 to subtract, or\n\tEnter 3 to multiply\n\t-----------------------\n\t\tEnter: ";
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Invalid choice! Please try again." << endl;
continue;
}
else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " + " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " - " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " = ";
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "What is " << num1 << " * " << num2 << " = ";
cin >> guess;
}
else if (guess == num3)
cout << "That is correct!" << endl;
}
while (true)
{
char play;
cout << "Would you like to play again? Press Y for yes or Q for quit: ";
cin >> play;
if (play == 'Y' || play == 'y')
break;
else if(play == 'Q' || play == 'q')
{
loop = false;
cout << "Good bye.\n";
break;
}
else
cout<< "Invalid choice.\n";
}
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
布尔循环=真;
while(循环)
{
//生成两个随机单位数整数btwn 0-9
srand(时间(0));
int num1=rand()%10;
int num2=rand()%10;
整数运算,play,num3,guess,Y,Q;
//如果num1 cout在第二个while循环中发现问题。play
变量应声明为