-2

我可以使用

gotofall-through

在 Java 中的Switch 语句中,就像在 C# 中一样?

这是我在 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
4

1 回答 1

0

goto 在 Java 中不使用,尽管关键字列表确实有一个 goto 条目,但它没有被使用。相反,您可以使用breakcontinue

于 2018-02-28T04:51:47.313 回答