0

我有一个关于此编码中的故障的快速问题。

当我运行这个程序并进入这个循环时(通过输入 1 作为数字),一切都很顺利,除了即使我进入++generalcounter1;了循环体,它似乎并不是每次都在这个变量中添加一个循环播放。当变量 == 10 时,它永远不会运行它应该运行的消息。

请帮助我,我很沮丧!非常感谢您的建议!

抱歉,如果不清楚,我对这个网站以及编程都是新手。

while (number == 1){
 int addend1;
 int addend2;
 int sum;
 int answer1;
 int generalcounter1 = 0;
 int reply1;

 ++generalcounter1;

 addend1 = 1 + rand() % 50;
 addend2 = 1 + rand() % 50;

 sum = (addend1 + addend2);

 cout << "\nWhat is " << addend1 << " plus " << addend2 << "? ";
 cin >> answer1;

 if (answer1 == sum){
     reply1 = winstatement(); 
 }          
 else{
     reply1 = losestatement();
 }

 if (generalcounter1 == 10){

     if (rightanswers >= 8){
        cout << "\nCongratulations, you are ready to go to the next level!\n" << endl;
     }
     else{
        cout << "\nPlease ask your teacher for extra help.\n" << endl;
     }  
  }   
}
4

1 回答 1

4

您在generalcounter1循环的每次迭代中声明一个新的,递增它,并在迭代结束时将其销毁。将声明移到循环之外,每次只增加一个,而不是每次增加一个新的,在它增加两次之前就被销毁。

int generalcounter1 = 0;
while (number == 1) { 
    //loop body
}
于 2012-09-26T02:03:05.997 回答