简体   繁体   English

Java中的Switch语句失败?

[英]Switch statement fall-through in Java?

Can I use the我可以使用

goto or fall-through转到失败

in a Switch statement in Java like you can in C#?在 Java 中的Switch 语句中,就像在 C# 中一样?

Here is an example of the code I modified in C# in order to use the goto statement in a conditional Switch, pay attention where it says SAM:这是我在 C# 中修改的代码示例,以便在条件 Switch 中使用goto语句,注意它说 SAM 的地方:

      // add 1 to appropriate counter for specified grade
       private void IncrementLetterGradeCounter( int grade )
       {
          // determine which grade was entered
          switch ( grade / 10 )
          {
             case 9: // grade was in the 90s
             case 10: // grade was 100 
                ++aCount; // increment aCount
                    goto case 8; // SAM: in this case I omitted the break 
                //by commenting and use the "goto case <number>;" 
                //which let me continue without errors
                //break; // necessary to exit switch
             case 8: // grade was between 80 and 89
                ++bCount; // increment bCount    
                break; // exit switch
             case 7: // grade was between 70 and 79
                ++cCount; // increment cCount    
                break; // exit switch
             case 6: // grade was between 60 and 69
                ++dCount; // increment dCount    
                break; // exit switch
             default: // grade was less than 60
                ++fCount; // increment fCount       
                break; // exit switch
          } // end switch
       } // end method IncrementLetterGradeCounter

goto is not used in Java, though the Keywords list does have an entry for goto, it is not used. Java 中不使用 goto,尽管关键字列表确实有一个 goto 条目,但并未使用。 Instead, you can use break or continue相反,您可以使用breakcontinue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM