2015-04-23 93 views
0

我目前正在爲大學建立Java程序。其中一部分是建立一個菜單系統。導師會喜歡菜單上的異常處理。這是一個沒有用戶界面的簡單菜單。菜單系統的展望處理

該菜單是一個switch語句,用戶輸入他們想要的選項;

  1. 方案一
  2. 方案二
  3. 方案三

什麼導師想是一種嘗試捕捉方法,一個讓用戶可以輸入數字1或輸入「一「並且系統不會崩潰。

我已經在網上閱讀了無數篇文章,但我無法找到對此的幫助,或者它在我的頭上。任何幫助,這將是太棒了。

編輯: 因此,這是一年級,他只是希望功能下降,我們可以爲他跑過去。我現在可以獲取代碼。首先是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」或「一」和如果他們式IT系統不會崩潰

感謝很多提前 康納爾

+0

後與任何你嘗試,直至現在的問題的代碼。 – mbsingh

+0

沒有用戶界面的菜單?誰應該使用它呢? – glglgl

+0

使用代碼感謝主編編輯! – Conor606

回答

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 

    } 
} 
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 
    } 
}