2017-08-05 101 views
1

我對Java非常陌生,我試圖使用try-catch語句。我想添加一個try catch案例,但是當我添加它時,該消息僅打印一次並結束。我想重新打印:如何在開關盒上使用try-catch語句但循環開關盒?

System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations"); 
     System.out.println("Typing other numbers will end the Chatbot"); 

但程序剛結束。有沒有辦法循環try-catch語句?

Scanner userinput = new Scanner(System.in); 
    int startup; 
    //popup for 1 to chat, 2 to play and 3 to edit 
    while (true) { 
     try { 
      System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations"); 
      System.out.println("Typing other numbers will end the Chatbot"); 
      startup = userinput.nextInt(); 
      switch (startup) { 

       case 1: 
        ConversationBot chat = new ConversationBot(); 
        chat.ChattingBot(); 
        break; 
       case 2: 
        GameBot game = new GameBot(); 
        game.GamingBot(); 
        break; 
       case 3: 
        EditBot edit = new EditBot(); 
        edit.EditingBot(); 
        break; 
       default: 
        System.exit(0); 
      } 
     } catch (InputMismatchException e) { 
      System.out.println("Invalid User Input. Please enter a value from 0 to 4."); 
      break; 
     } 
     String returningCode = returnChoiceOfChatbot(startup); 
     System.out.println(returningCode); 
    } 

謝謝你的幫助。 BTW,這是returnChoiceOf聊天機器人方法

public static String returnChoiceOfChatbot(int input) { 
    String returnChoice = null; 
    switch (input) { 
     case 1: 
      returnChoice = ("You have chosen to chat with me!"); 
      break; 
     case 2: 
      returnChoice = ("you have chsen to play word games with me!"); 
      break; 
     case 3: 
      returnChoice = ("Please enter an input that you would give to the Chatbot."); 
      break; 
     default: 
      System.exit(0); 
    } 
    return returnChoice; 

}//end of returnChoice method  

回答

2

您需要在您的catch塊與continue;更換線break;。如果不是數字,您想要求用戶輸入新的輸入。否則,break會打破整個while循環並阻止其再次運行。這就是說,它應該閱讀:

} catch (InputMismatchException e) { 
     System.out.println("Invalid User Input. Please enter a value from 0 to 4."); 
     continue; // Jump back to the beginning of the while-loop 
    } 

另外檢查是否需要移動這兩條線:

String returningCode = returnChoiceOfChatbot(startup); 
System.out.println(returningCode); 

while循環之外。雖然我不清楚它們的用途,但看起來你可能想在while循環離開後只運行一次它們。

+0

給它一個',而(真)'和唯一的出方式它是一個'System.exit(0)',我會想象那些行是他們應該在的地方 – CupawnTae

+0

我會說他們應該每次都在循環運行,因爲它們與每次選擇 – CupawnTae

0
} catch (InputMismatchException e) { 
    System.out.println("Invalid User Input. Please enter a value from 0 to 4."); 
    break; 
} 

只要刪除break。它與catch沒有任何關係,具體而言,就是您在其中編寫的break

+0

看起來相關就像發生異常時'catch'不應該運行後的兩行一樣 - 因此無論是像Lars Blumberg所建議的'continue',還是將這些行移動到'try'中都是有意義的,而不是僅僅刪除'break' – CupawnTae

+1

@CupawnTae他們當然應該在'try'塊中。我永遠不會理解爲什麼人們在'try'塊之外放置'try/catch'延續。 – EJP

+0

同意,但實際上如果你「只是刪除'break'」它不會編譯,因爲'startup'在使用時可能沒有被初始化。所以你必須*移動代碼(是)或使用「繼續」(如在Lars的答案中) – CupawnTae

0

(無標籤用於指定什麼打出來的時候)的break聲明將退出最近switchwhilefordo .. while循環。

您一般會已將switch一起使用,以停止執行到下一個案例 - 例如,如果您沒有休息時間並且用戶選擇了1,則會執行所有三種情況的代碼,然後退出該程序。

但是在catch塊內,break退出while循環。由於意圖是告訴用戶他們的輸入是無效的,然後要求新的輸入,這不是你想要在這裏做的。您可以改變breakcontinue這將中止while循環的當前迭代並重新開始循環,但一般來說這種流量控制會使你的計劃難以遵循,因此維持。

我猜你把最後的break加入跳過returnChoiceOfChatbot(...)代碼時輸入無效。但這正是例外情況 - 當發生意外事件時中止正常的代碼流。所以,只要將「正常流動」的代碼全部try塊內,你將不再需要在所有break(或continue):

while (true) { 
    try { 
     System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations"); 
     System.out.println("Typing other numbers will end the Chatbot"); 
     startup = userinput.nextInt(); 
     switch (startup) { 
      // cases in here as before, omitted for brevity 
     } 
     String returningCode = returnChoiceOfChatbot(startup); 
     System.out.println(returningCode); 
    } catch (InputMismatchException e) { 
     System.out.println("Invalid User Input. Please enter a value from 0 to 4."); 
    } 
}