簡體   English   中英

我如何使用帶有switch case的try-catch語句但循環swich case?

[英]How do i use try-catch statement with switch case but loop the swich case?

我是Java的新手,我試圖使用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 Chatbot方法

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    

你需要更換break; continue; catch塊。 如果用戶不是數字,您想要詢問用戶輸入新內容。 否則該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循環離開后運行它們一次。

break的語句(無標簽來指定哪些突圍,使用時)將退出最接近的switchwhilefordo .. while循環。

您通常必須使用它與switch一樣停止執行到下一種情況 - 例如,如果您沒有中斷並且用戶選擇1 ,它將執行所有三種情況的代碼,然后退出該程序。

然而,在catch塊中, break退出while循環。 由於目的是告訴用戶他們的輸入無效然后請求新的輸入,這不是你想要做的。 可以break更改為continue ,這將中止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.");
    }
}
} catch (InputMismatchException e) {
    System.out.println("Invalid User Input. Please enter a value from 0 to 4.");
    break;
}

只需刪除break 它與catch沒有任何關系,只是你在其中寫的break

暫無
暫無

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

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