我可以使用
吗?转到或转发
在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