|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <cmath>
using namespace std;
void repeat(int, int);
void repeat(int a, int b)
{
int answer;
cin >> answer;
while (answer!=a*b)
{
cout << "Please once again!" << endl;
repeat(a, b);
}
cout << "Yeah!!" << endl;
}
void praise();
void praise()
{
int c = rand() % 4 + 1;
switch (c)
{
case 1:cout << "Very good!\n"; break;
case 2:cout << "Excellent!\n"; break;
case 3:cout << "Nice work!\n"; break;
case 4:cout << "Keep up the good work!\n"; break;
}
}
void criticize();
void criticize()
{
int c = rand() % 4 + 1;
switch (c)
{
case 1:cout << "No no~ again please!\n"; break;
case 2:cout << "Wrong. Try once more\n"; break;
case 3:cout << "Don't give up~\n"; break;
case 4:cout << "No no no.Keep trying!\n"; break;
}
}
int main()
{
cout << "Welcome to 小学生乘法训练~\n";
while (1)
{
int a = rand() % 10, b = rand() % 10;
int answer;
cout <<"\n"<< a << " times " << b << " is ?" << endl;
cin >> answer;
if (answer == a * b)
{
praise();
}
else
{
criticize();
repeat(a, b);
}
}
return 0;
}
是一个循环出简单乘法的例子,
我的思路是如果输入错误执行repeat函数,然后直到对了为止。
可如果一道题出错两次递归就结束不了了?
我哪些写错了呀~
本帖最后由 liuzhengyuan 于 2020-3-9 19:19 编辑
刚刚弄错了,这应该没问题
- #include<iostream>
- #include<cmath>
- using namespace std;
- int repeat(int a, int b)
- {
- int answer;
- cin >> answer;
-
- while (answer!=a*b)
- {
- cout << "Please once again!\n" << endl;
- return repeat(a, b);
- }
- cout << "Yeah!!\n" << endl;
-
- }
- int main()
- {
- cout << "Welcome to 小学生乘法训练~\n";
- while (1)
- {
- int a = 5, b = 5;
- int answer;
- cout <<"\n"<< a << " times " << b << " is ?" << endl;
- cin >> answer;
- if (answer == a * b)
- {
- cout<<"correct\n";
- break;
- }
- else
- {
- cout<<"wrong\n";
- repeat(a, b);
- break;
- }
- }
- }
复制代码
|
|