switch语句始终始终通过默认值

时间:2020-04-18 19:31:29

标签: c++ switch-statement

大家好,正如标题所示,当我运行代码时,一切正常,可以正确循环,但最终以某种方式最终在重新启动循环之前选择了默认情况。

由于它很长,我将通过pastebin发布我的代码:

https://pastebin.com/TxvH3MhJ

  while(myScore != "new" || myScore != "NEW" || myScore != "New"){

        switch(s2){
         case 1 :
          switch(s3){
          // code
           }


          default :
                cout<<("\nSomething went wrong, please restart the program.");   
                break; //this statement works correctly.

            }


        default : //this statement is always brought up each time i type "new", i also added a cout s2 to check if actually somehow the variable was changing mid code but it prints out correctly.
        cout<<("\nSomething went wrong please restart the program.");
        cout<<s2;
        break;`

p.s。我是一个初学者,请记住,尽管代码可以完成工作,但它可能非常初级。目前,如果您输入1,代码就可以正常工作,因为我只是在计算逻辑。

1 个答案:

答案 0 :(得分:0)

break;仅脱离最里面的开关(或循环)。因此,您也需要将其放置在内部switch之后(除非您需要掉线),例如:

switch(s1) {
case 1:
    switch(s2) {
    ...
    }
    break; // <== HERE
case 2:
    ...
    break;
default:
    ....
    break; // optional
}