如何在 for 循环中使用 switch case?
How to use a switch case inside a for loop?
我正在用 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() ;
}
问题不是循环 + switch
,而是您使用的无限递归:
display_question()
呼叫 question()
question()
呼叫 quiz_count()
quiz_count()
调用了 display_question()
,所以您回到了第 1 步。
您观察到的 i
的值只是对 display_question
函数的不同调用的值。
通过从 quiz_count
中删除 display_question()
调用,您可能会得到想要的结果。然而,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 () ;
}
我正在用 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() ;
}
问题不是循环 + switch
,而是您使用的无限递归:
display_question()
呼叫question()
question()
呼叫quiz_count()
quiz_count()
调用了display_question()
,所以您回到了第 1 步。
您观察到的 i
的值只是对 display_question
函数的不同调用的值。
通过从 quiz_count
中删除 display_question()
调用,您可能会得到想要的结果。然而,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 () ;
}