C++循环:如何在用户输入无效输入时多次重复程序
我几乎完成了这项任务,但仍然无法让代码连续显示错误消息,直到输入正确的输入 例如,如果用户为操作输入4,该值应介于1-3之间,则正确显示:您的操作选择无效!请使用1、2或3重试。但是,如果用户为操作输入另一个无效数字,如5,则不会重复错误消息,而是继续向前 有谁能帮我找出如何让每个错误消息重复,直到为每个提示输入有效的数字或字符 注意:我对编码非常陌生,并且仍然了解stackoverflow…我想我已经遵循了所有MCVE建议/格式。谢谢 您的do while循环C++循环:如何在用户输入无效输入时多次重复程序,c++,loops,debugging,if-statement,continue,C++,Loops,Debugging,If Statement,Continue,我几乎完成了这项任务,但仍然无法让代码连续显示错误消息,直到输入正确的输入 例如,如果用户为操作输入4,该值应介于1-3之间,则正确显示:您的操作选择无效!请使用1、2或3重试。但是,如果用户为操作输入另一个无效数字,如5,则不会重复错误消息,而是继续向前 有谁能帮我找出如何让每个错误消息重复,直到为每个提示输入有效的数字或字符 注意:我对编码非常陌生,并且仍然了解stackoverflow…我想我已经遵循了所有MCVE建议/格式。谢谢 您的do while循环 do { cout &l
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
}
while (operation > 3 || operation < 1);
应该是
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
}
}
while (operation > 3 || operation < 1);
如果只想给用户一次重新尝试以选择正确的操作,请不要使用Do while循环。把那个街区往这边走
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
或
如果只想打印第一次错误尝试和所有成功错误尝试的错误消息,可以通过以下方式执行:
int flag=0;
do{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if ((operation > 3 || operation < 1)&&flag==0)
{
flag=1;
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
} while(operation > 3 || operation < 1);
P29:练习算术技能,如果/否则,循环
说明:
写一个程序让孩子练习算术技能
程序应该首先询问需要什么样的练习:+、-、*,并让用户根据需要重复练习多次,直到输入Q
将从0-9生成两个随机数
如果孩子回答正确,就会出现一条信息,然后他们可以转到下一个问题,生成两个不同的数字
如果孩子回答不正确,就会出现一条消息&问题应该重复出现,使用相同的数字
终于修好了!:
.我想我已经遵循了MCVE的所有建议/格式。M代表最小值,因此您的开关可以被移除,只需跳过您的内部操作即可。感谢您阅读我想您是这样做的?你已经知道了,但是你忘了跟随。如果你愿意的话,你可以把它作为一个评论发表,尽管如果这个问题已经很好了,理论上它不应该被否定谢谢@Jarod42!这非常有用!
int flag=0;
do{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if ((operation > 3 || operation < 1)&&flag==0)
{
flag=1;
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
} while(operation > 3 || operation < 1);
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
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;
cout << "" << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
}
}while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (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;
cout << "" << 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;
cout << "" << 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;
}