2014-10-12 53 views
1

上午建立書店系統 其中一種方法會要求用戶輸入書名然後說明書的代碼。 我的問題是,我怎麼能回想起來,如果有「InputMismatchException」。 我的意思是捕獲將打印到secreen「錯誤的輸入!」但我想提示用戶重新輸入使用try-catch,如果輸入錯誤,我該如何再次回想一遍?

這裏是我的代碼

public void add(){ 
    try{ 
    System.out.println("Enter the book code :"); 
    int c = s.nextInt(); 
    try{ 
     System.out.println("Eneter the book quantity"); 
     int q = s.nextInt(); 

     try{ 
      System.out.println("Eneter the book description"); 
      String d = s.next(); 

      try{ 
       System.out.println("Eneter the book cost price"); 
       double cp = s.nextDouble(); 

       try{ 
        System.out.println("Eneter the book selling price"); 
        double sp = s.nextDouble(); 

        try{ 
         System.out.println("Enter status: [a for available - u for unvailable]"); 
         String input = s.next(); 
         if(input.equals("a")) { 
          System.out.println("available: "); 
         } 
         else if (input.equals("u")) { 
          System.out.println("unavailable "); 
         } 
         else { 
          System.out.println("Wrong Input"); 
         } 

         try{ 
          System.out.println("enter the discount on the item \"if exsist\""); 
          double dis = s.nextDouble(); 
         }catch(InputMismatchException e){ 
          System.out.println("Wrong Input"); 

         } 
        }catch(InputMismatchException e){ 
         System.out.println("Wrong input"); 
        } 
       }catch(InputMismatchException e){ 

       } 
      }catch(InputMismatchException e){ 
       System.out.println("WRONT INPUT!!"); 
      } 
     }catch(InputMismatchException e){ 
      System.out.println("WRONG INPUT!!"); 
     } 
    } catch(InputMismatchException e){ 
     System.out.println("Wrong input. NUMBERS only!!"); 
    } 
} catch(InputMismatchException e){ 
    System.out.println("You have to enter a NUMBER only"); 

} 
+3

這些爲什麼嵌套? – 2014-10-12 04:06:04

+5

你正在提示的每件作品都應該是它自己的一種方法。在該方法內部,處理循環直到獲得有效的輸入(或退出標誌)。當方法返回時,它將返回有效的輸入(可能需要幾次嘗試)或返回結束輸入的標誌(可能返回'null')。 – Jared 2014-10-12 04:06:07

+1

HOLY COW GUACAMOLE – Tdorno 2014-10-12 04:15:59

回答

0

你需要某種類型的循環,無論是whiledo-while

boolean invalidInput; 
do { 
    invalidInput = false; 
    try { 
     System.out.println("Enter the book quantity"); 
     int q = s.nextInt(); 
    } catch (InputMismatchException e) { 
     System.out.println("Please enter a valid integer."); // this is more polite 
     invalidInput = true; // This is what will get the program to loop back 
     s.nextLine(); 
    } 
} while (invalidInput); 

如果您這樣做,則不需要嵌套所有try/catch塊;執行一個輸入直到它有效,然後執行下一個輸入等。

正如一位評論者所說,最好將每個輸入放入一個方法中。如果你這樣做,你不需要boolean

private int getBookQuantity() { 
    while (true) { 
     try { 
      System.out.println("Enter the book quantity"); 
      int q = s.nextInt(); 
      return q;   
     } catch (InputMismatchException e) { 
      System.out.println("Please enter a valid integer."); 
      s.nextLine(); 
     } 
    } 
} 

這是一個「無限循環」的時候,return語句返回一個有效的值退出。更妙的是可能是寫只有一個方法用於輸入int值(和別人doubleString):

private int getInteger(String whatToEnter) { 
    while (true) { 
     try { 
      System.out.println("Enter the " + whatToEnter); 
      int q = s.nextInt(); 
      return q;   
     } catch (InputMismatchException e) { 
      System.out.println("Please enter a valid integer."); 
      s.nextLine(); 
     } 
    } 
} 

編輯:對不起,我忘了nextLine()是無效的輸入之後需要;這會拋出無效整數和其他所有行,這樣它不會重複導致另一個異常。

+0

如果輸入不正確,所有的解決方案將繼續循環「無限循環」。 – mazin 2014-10-12 05:04:38

+0

你確實幫了我一些忙,謝謝:) – mazin 2014-10-12 05:05:07

+0

@mazin我忘了需要'nextLine()'。請參閱我的編輯。 – ajb 2014-10-12 05:45:51

相關問題