练习时,系统随机出题,键入结果,正确和错误均有提示,出错时允许再输入,
加减乘除运算,练习时最多二次机会,若还不正确,给出答案。继续出题,按ESC,显示总题数,正确数和
正确率。
这是本人写的一段练习时的代码,不知怎么样做到出错时允许再输入,最多两次机会(づ。‿。)づ
求各位大大指点~~~~(>_<)~~~~
加减乘除运算,练习时最多二次机会,若还不正确,给出答案。继续出题,按ESC,显示总题数,正确数和
正确率。
这是本人写的一段练习时的代码,不知怎么样做到出错时允许再输入,最多两次机会(づ。‿。)づ
求各位大大指点~~~~(>_<)~~~~
void practisesys(void) { int n1,n2,result,type,input,right,wrong; right = 0; wrong = 0; type = typechoose(); system("cls"); do { do { srand((unsigned)time(NULL)); //初始化随机数 n1 = rand()%100; n2 = rand()%100; }while(n1<n2); label1: if(type == 1) { result = Addition(n1,n2); cout<<n1<<" + "<<n2<<"="; } else if(type == 2) { result = Subtraction(n1,n2); cout<<n1<<" - "<<n2<<"="; } else if(type == 3) { result = Multiplication(n1,n2); cout<<n1<<" * "<<n2<<"="; } else if(type == 4) { result = Division(n1,n2); cout<<n1<<" / "<<n2<<"="; } else if(type == 5) { srand((unsigned)time(NULL)); type = rand()%4+1; goto label1; } cin>>input; if(input == result) { right++; cout<<"回答正确!\n"; } else { wrong++; cout<<"回答错误!\n"; cout<<"正确答案是:"<<result<<endl; } }while(1); cout<<"你已经答对的题目有"<<right<<endl; cout<<"你总共回答题目有"<<right+wrong<<endl; cout<<"你的正确率为百分之"<<right/((right+wrong)*100)<<endl; cout<<"总分为"<<right*10<<endl; cout<<"谢谢使用此系统!\n"; getch(); return; }
解决方案
200
试试这样:
void practisesys(void) { int n1, n2, result, type, input, right, wrong; right = 0; wrong = 0; type = typechoose(); system("cls"); do { do { srand((unsigned)time(NULL)); //初始化随机数 n1 = rand() % 100; n2 = rand() % 100; } while (n1<n2); label1: if (type == 1) { result = Addition(n1, n2); cout << n1 << " + " << n2 << "="; } else if (type == 2) { result = Subtraction(n1, n2); cout << n1 << " - " << n2 << "="; } else if (type == 3) { result = Multiplication(n1, n2); cout << n1 << " * " << n2 << "="; } else if (type == 4) { result = Division(n1, n2); cout << n1 << " / " << n2 << "="; } else if (type == 5) { srand((unsigned)time(NULL)); type = rand() % 4 + 1; goto label1; } while (1) { cin >> input; if (input == result) { right++; cout << "回答正确!\n"; break; } else { wrong++; cout << "回答错误!\n"; if (wrong == 2) { cout << "正确答案是:" << result << endl; break; } } } } while (1); cout << "你已经答对的题目有" << right << endl; cout << "你总共回答题目有" << right + wrong << endl; cout << "你的正确率为百分之" << right / ((right + wrong) * 100) << endl; cout << "总分为" << right * 10 << endl; cout << "谢谢使用此系统!\n"; getch(); return; }