2017-08-05 48 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."); 
    } 
}