[英]Switch statement fall-through in Java?
我可以使用
轉到或失敗
在 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
Java 中不使用 goto,盡管關鍵字列表確實有一個 goto 條目,但並未使用。 相反,您可以使用break
或continue
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.