Switch语句在Java中失败了?

Switch语句在Java中失败了?,java,c#,switch-statement,goto,fall-through,Java,C#,Switch Statement,Goto,Fall Through,我能用这个吗 转到或失败 在Java中的Switch语句中,就像在C#中一样 下面是我在C#中修改的代码示例,为了在条件开关中使用goto语句,请注意其中的SAM: // add 1 to appropriate counter for specified grade private void IncrementLetterGradeCounter( int grade ) { // determine which grade was

我能用这个吗

转到失败

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

下面是我在C#中修改的代码示例,为了在条件开关中使用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
//为指定等级的相应计数器添加1
专用void IncrementLetterGradeCounter(内部等级)
{
//确定输入的年级
道岔(10级)
{
案例9://年级在90年代
病例10://等级为100
++aCount;//递增aCount
转到案例8;//山姆:在这种情况下,我忽略了中断
//通过评论和使用“goto案例”
//这让我继续没有错误
//break;//退出开关所必需的
病例8://等级在80到89之间
++bCount;//增量bCount
break;//退出开关
病例7://等级在70到79之间
++cCount;//增量cCount
break;//退出开关
病例6://等级在60到69之间
++dCount;//增量dCount
break;//退出开关
默认://等级小于60
++fCount;//增量fCount
break;//退出开关
}//结束开关
}//end方法IncrementLetterGradeCounter

在Java中不使用goto,尽管列表中确实有goto条目,但不使用它。相反,您可以使用
break
continue

请告诉我这是不合法的。您想在这里实现什么?当你得到9或10分时,a和b计数都递增?@ElliottFrisch不幸的是,这可能是你问题的答案。试着先读一下,你不能这样做,但你可以通过调用案例中的a函数来重用开关。Elliott goto在C#中是合法的,我发布的代码在C#中运行时没有问题,但是如果我没有指定goto并且我没有注释它的中断,它将出现一个错误:“控制不能从一个案例标签中失败”。我只是想知道在Java中是否有类似的功能。我提供了代码作为示例。或者两者都不提供,如果你想失败的话。我知道“goto”在Java中是一个“保留关键字”,并且是一个未使用的关键字。这意味着不能在代码中用于变量或其他构造的名称。我在这里的目的是使用C#Switch语句找到任何帮助,为我提供与“goto”或“fall-through”相当的帮助。我进行了搜索,找到了以下链接: