为什么这个节目打印"九"两次?

时间:2017-01-13 03:37:20

标签: c++

实时版:http://cpp.sh/953y6

代码:

eight
nine
nine
even
odd

数字8到11在for循环中循环。 if(n <= 9)只应触发两次,当n为8且n为9.而输出为:

INSERT INTO test VALUES ('2-03-2016') 

为什么?

5 个答案:

答案 0 :(得分:14)

因为你没有break及其失败案件。

case 8: cout << "eight\n";  // <-- need break here
case 9: cout << "nine\n";   // otherwise it's fall-through to here even input is 8
  • 良好做法是在每break case switch之后总是 var content={ html:'<div>Example text here</div><div><br></div><div id="jsContent"></div>', action:state actionTarget="jsContent" } document.body.innerHTML+=content.html; document.getElementById(content.actionTarget).innerHTML=content.action();
  • 如果您需要故意进行堕落,请明确说明 关于那次失败的评论。

答案 1 :(得分:7)

因为你的交换机案例中没有break语句:

if(n <= 9){
        switch(n){
            case 1: cout << "one\n";
            case 2: cout << "two\n";
            case 3: cout << "three\n";
            case 4: cout << "four\n";
            case 5: cout << "five\n";
            case 6: cout << "six\n";
            case 7: cout << "seven\n";
            case 8: cout << "eight\n";
            case 9: cout << "nine\n";
        }

  }

当案例8被调用时,它首先打印8,然后通过落到案例9并打印9。然后在n为9时调用案例9,再次打印9。在以下情况之后添加break语句:

if(n <= 9){
        switch(n){
            ...
            case 8: cout << "eight\n";
                    break;
            case 9: cout << "nine\n"; //last case, dont really need a break
        }

}

最好在切换后的每一个案例之后加上休息,除非是故意的。

答案 2 :(得分:2)

为了避免将来出现此类问题,您应该了解最后没有实现定义隐式 break;用例每个case :语句。

请考虑以下示例,了解“fall-through”的好处

switch (day) {  
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        cout << "Weekday";  //same for all five mentioned days of the week.
        break;   // << yes, you need a explicit "break" statement to stop this fall through
    case SATURDAY:
    case SUNDAY:
        cout << "yeah! it's a Weekend";  //same for Sat and Sun.
        break;  //not necessary, but a good practice.
}

答案 3 :(得分:0)

开关案例的基本规则是如果你不打破它之后它也会进一步打印。 所以在这里你没有突破

Case 8 : cout << "eight\n"
//Put break here
Case 9: cout << "nine\n"

因此,对于单打印9,您必须在案例8之后放置。

答案 4 :(得分:0)

您的代码应如下所示

int main() {
    // Complete the code.
    int num1 = 8, num2 = 11;
    for(int n = num1; n <= num2; n++){

        if(n <= 9){
            switch(n){
                case 1: cout << "one\n";
                break;
                case 2: cout << "two\n";
                break;
                case 3: cout << "three\n";
                break;
                case 4: cout << "four\n";
                break;
                case 5: cout << "five\n";
                break;
                case 6: cout << "six\n";
                break;
                case 7: cout << "seven\n";
                break;
                case 8: cout << "eight\n";
                break;
                case 9: cout << "nine\n";
                break;
            }
      }
      else if(n % 2 == 0){ //even
            cout << "even\n";
      }
      else if(n > 9 && n %2 == 1){ //odd
            cout << "odd\n";
      }

   }
    return 0;
}