2015-09-30 135 views
3

我最近開始學習Java,在測試某些東西時遇到了問題。這可能是一個非常簡單的問題,但我似乎無法解決它。 這是我的代碼:如果在while循環中,不能跳出while循環

int firstj = 1; 

    if (firstj == 1) { 
     String choice = "Type a number between 1 and 4"; 
     System.out.println(choice); 
     while (true) { 

      if (firstj == 1) { 
       Scanner third = new Scanner(System.in); 
       String thirdch = third.nextLine(); 

       while (true) { 

        if (thirdch.equals("1")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } else if (thirdch.equals("2")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } else if (thirdch.equals("3")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } 

        else if (thirdch.equals("4")) { 
         // I need this to break the loop and move on to the 
         // "Done." string 
         break; 
        } 

        else { 
         System.out.println("Type a number between 1 and 4"); 
         thirdch = third.nextLine(); 
        } 
       } 

      } 
     } 
    } 
    String done = "Done"; 
    System.out.println(done); 

我想讓它這樣,當你鍵入1,2或3,你得到的字符串,告訴您再次輸入一個號碼,接受用戶的輸入,而當你鍵入4循環會中斷並轉到完成的字符串。如果你能用簡單的代碼幫助我解決這個問題,我將不勝感激,因爲我不知道任何更先進的東西。

+0

你的意思是你想突破* outer *循環嗎? –

+0

你正在打破內在,而不是外循環。所以雖然(真)永遠是真的。 – Casey

+0

「break」只能讓你擺脫1循環。你最根本的問題是無限循環。有一個無限循環已經夠糟了,你實際上有2個。現在是重構代碼的時候了。 –

回答

7

您可以標記循環,然後使用break語句中的標籤指定要跳出哪個循環,例如,

outer: while (true) { 
    while (true) { 
    break outer; 
    } 
} 
1

這是不是很從描述清楚,但continue會讓你跳到循環結束,繼續與循環的其餘部分,你可以使用標誌來控制,如果你想退出多個級別。

5

爆發嵌套循環的最具可擴展性的方法是將整個事物放入函數中並使用return

在Java中打破標籤是另一種選擇,但它可能使代碼變得脆弱:你受到一些頑固的重構者的擺佈,他們可能覺得傾向於將「打破標籤」移動到幸福地不知道後果;編譯器無法警告這些惡意軟件。

+1

也許你可以舉一些脆弱性的例子來提防?這種'不確定的惡劣'在被咬傷之前很難發現。 –

+0

我認爲Bathsheba只是試圖指出,當添加更多的循環並且代碼變得更大和更復雜時,break語句的這種用法很難遵循,我完全同意他的觀點。 –

+0

@ChristopherYang我不會不同意:我實際上還記得上次我用標籤打破時間;我認爲通常有更簡單的方法來構建代碼以避免它。我的觀點是,對那些不熟悉它創建的問題的人來說,描述代碼是脆弱的是什麼意思是很有用的。 –

2

你可以使用一個鎖停下來,這樣的:

public static void main(String[] args) throws Exception { 
    int firstj = 1; 
    if (firstj == 1) { 
     boolean lock = true; 
     while (lock) { 

      if (firstj == 1) { 
       Scanner third = new Scanner(System.in); 

       while (lock) { 

        System.out.println("Type a number between 1 and 4"); 
        String thirdch = third.nextLine(); 
        switch (thirdch) { 
        case "1": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "2": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "3": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "4": 
         lock = false; 
         break; 
        } 

       } 
       third.close(); 

      } 
     } 
    } 
    String done = "Done"; 
    System.out.println(done); 
} 

我希望它能幫助。