菜单系统的预期处理

时间:2015-04-23 09:34:54

标签: java exception-handling switch-statement

我目前正在为大学建立一个Java程序。部分原因是建立一个菜单系统。导师希望在菜单上进行异常处理。这是一个没有用户界面的简单菜单。

菜单是一个switch语句,用户输入他们想要的选项;

  1. 选项一
  2. 选项二
  3. 选项三
  4. 导师想要的是一个try catch方法,一个用户可以输入数字1或输入“One”,系统不会崩溃。

    我在网上看过无数篇文章,但我要么找不到这方面的帮助,要么就是我的头脑。任何帮助都会很棒。

    编辑: 所以这是第一年的课程,他只是希望功能下降,我们可以为他做好准备。我现在可以得到代码。首先是PremiumAccount类,然后主菜单在程序类中调用每个方法。您可以在这里找到下面的菜单,如果需要,我也可以提供PremiumAccount课程。

    做{

            System.out.println("Premium Phone!");
            System.out.println("");
            System.out.println("Please select the option for which you would like to do!");
            System.out.println("1. Make a Call");
            System.out.println("2. Send a Text");
            System.out.println("3. Top Up");
            System.out.println("4. Display your details");
            System.out.println("5. Get your balance");
            System.out.println("6. Get your minutes used");
            System.out.println("7. Get your texts sent");
            System.out.println("8. Update the Call Costs");
            System.out.println("9. Update the Text Costs");
            System.out.println("10. Update the discount rate");
            System.out.println("11. Exit");
            intMenuChoice = numInput.nextInt(); //user input menu choice
    
            switch(intMenuChoice)
            {
                case 1: System.out.print("Please enter the amount length of the call in minutes: ");
                        intMinutesUsed = numInput.nextInt();
                        System.out.println(pre123.makeCall(intMinutesUsed));
                        System.out.println("");
                    break;
                case 2: System.out.print("Please enter the amount of characters used in the text: "); 
                        intCharactersUsed = numInput.nextInt();
                        System.out.println(pre123.sendText(intCharactersUsed));
                        System.out.println("");
                    break;
                case 3: System.out.print("Please enter the amount you would like to Top Up: ");
                        dblTopUp = numInput.nextDouble();
                        System.out.println(pre123.topUp(dblTopUp));
                        System.out.println("");
                    break;
                case 4: System.out.println("***Account Details***");
                        pre123.displayAccountDetails();
                        System.out.println("");
                    break;
                case 5: System.out.println("Account Balance: £" +df.format(pre123.getBalance()));
                        System.out.println("");
                    break;
                case 6: System.out.println("Minutes Used: " +pre123.getMinutes());
                        System.out.println("");
                    break;
                case 7: System.out.println("Texts Used: " +pre123.getTexts());
                        System.out.println("");
                    break;
                case 8: System.out.print("Please enter the new Cost of a Call: ");
                        newCallCost = numInput.nextDouble();
                        System.out.println(pre123.updateCallCost(newCallCost));
                        System.out.println("");
                    break;
                case 9: System.out.println("Please enter the next Cost of a Text: ");
                        newTextCost = numInput.nextDouble();
                        System.out.println(pre123.updateTextCost(newTextCost));
                        System.out.println("");
                    break;
                case 10: System.out.println("Please enter the new rate of Discount");
                         newDiscount = numInput.nextDouble();
                         System.out.println(pre123.setNewDiscount(newDiscount));
                         System.out.println("");
                    break;
                case 11: System.out.println("Have a nice Day!");
                         menuStatus = 1;
                    break;
    
                default: System.out.println("Please select a number between 1 - 11");
    
            }//end switch statement
    
    
        }while(menuStatus == 0);//end do while
    

    我们只需要按照我的说法让用户能够输入他们想要的但是尝试捕获,这样他们就可以输入“1”或“一个”,如果他们键入它,系统不会崩溃< / p>

    提前多多感谢 康纳

2 个答案:

答案 0 :(得分:0)

您可以使用Integer.parseInt()并抓住NumberFormatException,然后检查String

编辑后

您可以使用intMenuChoice = numInput.nextInt();代替String input = numInput.nextLine();,然后添加以下内容

try{
   intMenuChoice = Integer.parseInt(input);
}catch (NumberFormatException e){
   switch(input.toLowerCase()){
      case "one":
         intMenuChoice = 1;
         break;
      ......//So on

   }
}

答案 1 :(得分:0)

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String input = "";

    System.out.print("Enter the menu item: "); 
    input = scan.NextLine();

    try{
        if(input.equals("One") || input.equals("1"))
            doOptionOne();
        else
            throw new Exception("The menu option is not 1 or One"); //throws exception if the choice is not valid 
    }
    catch(Exception e){
        System.out.println(e.getMessage()); //print the exception message
    }
}