C++ 在没有程序的情况下询问用户同样的事情被终止c++;
我的程序要求用户回答两个随机数之和,如果答案正确,则输出“良好”,如果答案不正确,则“重试”。但我希望程序不断地问问题,当用户回答时,我的程序终止,你能帮我吗 我希望输出像这样C++ 在没有程序的情况下询问用户同样的事情被终止c++;,c++,C++,我的程序要求用户回答两个随机数之和,如果答案正确,则输出“良好”,如果答案不正确,则“重试”。但我希望程序不断地问问题,当用户回答时,我的程序终止,你能帮我吗 我希望输出像这样 How much is 19 plus 7 (-1 to End)?: 25 No - Please try again: 26 Very good! How much is 9 plus 12 (-1 to End)?: 21 Very good! How much is 71 plus 15 (-1 to End)?
How much is 19 plus 7 (-1 to End)?: 25
No - Please try again: 26
Very good!
How much is 9 plus 12 (-1 to End)?: 21
Very good!
How much is 71 plus 15 (-1 to End)?: 1
No - Please try again: 86
Very good!
How much is 17 plus 33 (-1 to End)?: -1
我的代码:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int number1,number2, answer;
int x=0 ;
srand(time(NULL));
number1 = rand() % 100 + 1;
number2 = rand() % 100 + 1;
x=number1 + number2;
cout << "What is the " << number1 << " plus " << number2 << " ?" << endl;
cin >> answer;
if (answer!=x)
cout << "No,Please try again.";
else
cout << "Very good!";
cin.clear();
cin.ignore();
cin.get();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数1,整数2,答案;
int x=0;
srand(时间(空));
number1=rand()%100+1;
number2=rand()%100+1;
x=数字1+数字2;
coutintmain()
{
整数1,整数2,答案;
int x=0;
srand(时间(空));
做
{
number1=rand()%100+1;
number2=rand()%100+1;
x=数字1+数字2;
不能在您的if-else条件下使用此选项
while(answer!=x)
{ cout<<"no! please try again";
cin>>answer;
}
cout<<"good";
while(答案!=x)
{coutanswer;
}
你想要的是一个循环。你需要一个循环。谢谢,我试着做while循环,但我只是不知道while的括号里写什么,现在我明白了逻辑,谢谢!
while(answer!=x)
{ cout<<"no! please try again";
cin>>answer;
}
cout<<"good";