我正在用 C 編程語言做一個測驗程式。我使用了 for 回圈來遍歷每個 switch case,但是當我運行程式時,它只是繼續回圈 case 0,并且在我回答我的測驗后無法停止回圈。我該如何解決?
#include <iostream>
#include <string>
using namespace std ;
void quiz_count () ;
void display_question () ;
void question (string question , string a , string b , string c , string d , char correct_answer) ;
void result () ;
int question_num = 0 ;
int correct = 0 ;
int wrong = 0 ;
int main ()
{
display_question() ;
return 0 ;
}
void quiz_count ()
{
system("cls") ;
cout << "Question Number: " << question_num << "\t\t Correct Answer:" << correct << "\t\t Wrong Answer:" << wrong << endl << endl ;
display_question () ;
}
void display_question()
{
for (int i=0; i<10 ; i )
{
switch (i)
{
case 0 :
question ( "1) What is recycling?" , "Buying new clothes" , "Collecting and using materials to make something new" , "Throwing things in garbage can" , "Selling items" , 'b' ) ;
break ;
case 1 :
question ( "2) What are the 3R's of the recycling?" , "Redirect, Rude, Round" , "Respectful, Responsible, Right" , "Reduce, Reuse, Recycle" , "Rewrite, Rewind, Respond" , 'c') ;
break ;
case 2 :
question ( "3) What goes into the green bin?" , "plastic" , "glass" , "cans" , "paper" , 'b' ) ;
break ;
}
}
result () ;
}
void result ()
{
system("cls") ;
cout << "The total of question is :" << question_num << endl ;
cout << "The correct answer from you is :" << correct << endl ;
cout << "The wrong answer from you is :" << wrong << endl ;
}
void question (string question , string a , string b , string c , string d , char correct_answer)
{
cout << question << endl ;
cout << "A. \t" << a << endl ;
cout << "B. \t" << b << endl ;
cout << "C. \t" << c << endl ;
cout << "D. \t" << d << endl ;
char answer ;
cout << "Please enter your answer here :" ;
cin>>answer ;
if (answer == correct_answer)
{
correct ;
}
else
wrong ;
question_num ;
quiz_count() ;
}
uj5u.com熱心網友回復:
問題不在于回圈 switch,而在于您使用的無限遞回:
display_question()電話question()question()電話quiz_count()quiz_count()calldisplay_question(),所以你回到了第 1 步。
值i您看到的只是對不同的呼叫到該值display_question的功能。
通過從 中洗掉display_question()呼叫,您可能會獲得所需的結果quiz_count。然而,loop 和 的組合在switch這里并不是一個好的選擇。除了更容易理解之外,以下實作產生了相同的結果:
void display_question()
{
question ( "1) What is recycling?" , "Buying new clothes" , "Collecting and using materials to make something new" , "Throwing things in garbage can" , "Selling items" , 'b' ) ;
question ( "2) What are the 3R's of the recycling?" , "Redirect, Rude, Round" , "Respectful, Responsible, Right" , "Reduce, Reuse, Recycle" , "Rewrite, Rewind, Respond" , 'c') ;
question ( "3) What goes into the green bin?" , "plastic" , "glass" , "cans" , "paper" , 'b' ) ;
result () ;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/402083.html
上一篇:如何找到矩陣中只出現一次的數字?
