簡體   English   中英

switch-case 語句有什么問題?

[英]What's wrong with the switch-case statement?

我不知道為什么 eclipse 用紅色波浪線Syntax error on token "{", SwitchLabels expected after this token switch-case語句,說Syntax error on token "{", SwitchLabels expected after this token我嘗試了下面兩個發布的代碼,我收到了同樣的抱怨。

代碼_1

switch (Test.h1.size()) {
    int size = Test.h1.size();

    case 1:
        break;

    case 2:
        break;
    }

代碼_2

switch (Test.h1.size()) {
    int size = Test.h1.size();

    case size == 1:
        break;

    case size == 2:
    for (int i=1; i<=Test.h1.size()-1; i++) {
        for (int j=i+1; j<=Test.h1.size(); j++) {
            System.out.println( Test.h1.get(i)+"+"+Test.h1.get(j)+"= "+((Test.h1.get(i))+(Test.h1.get(j))) );
        }
    }
        break;
    }

您的所有代碼都需要以防萬一。 你讀過文檔嗎? http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

嘗試這個:

int size = Test.h1.size();

switch (size) {
    case 1:
        break;

    case 2:
        ....;

}

要解決代碼 1 中的問題,您只需刪除int size = Test.h1.size(); .

對於代碼 2,您應該知道,在 Java 中您不能在 switch case 中使用布爾表達式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM