2014-03-06 68 views
0

我想程序重新做while循環,當它捕獲異常 - 例外是接收數字零。相反,它使用下面的代碼繼續一個while循環,我希望它再次詢問用戶輸入,直到用戶輸入一個不同於零的數字。嘗試捕獲異常,而循環

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class whilePerjashtim { 

    public static int division(int a, int b){ 

     return a/b; 
    } 

    public static void main(String[] args) { 

     Scanner s = new Scanner(System.in); 
     int a, b; 

     System.out.println("Enter a value: "); 
     a = s.nextInt(); 

    while(true){ 

     try 
     { 

      System.out.println("Enter b value"); 
      b = s.nextInt(); 

      System.out.println("Sum of division is: " + division(a,b)); 


     } 

     catch(ArithmeticException e) 
     { 
      System.err.println("Don't divide by zero!!!");  
     } 

     catch (java.util.InputMismatchException e) 
     { 
      System.err.println("Enter just a Number!!!"); 
     } 

     finally 
     { 
      System.out.println(); 
     } 

    } 

    } 
} 
+1

你如何_break_出循環?你如何繼續_循環? –

+0

你有沒有試過繼續使用! – vishram0709

+0

首先,你只需要服用一次,如果這是你想要的就可以了,但除非有例外,否則沒有任何中斷條件。 –

回答

1

使用以下形式的東西(不準確的Java爲您的家庭作業問題)

boolean validInput = false; 
while (!validInput) { 
    .. get input 
     .. set validInput = true if no error 
    .. catch error 
     .. print try again message 
} 
0

您可以設置一個布爾值,該值確定,如果while循環結束成功地。然後在每個循環中,首先假定值爲true,並且在引發異常時將其設置爲false

boolean success = false; 

while(success == false){ 
    success = true; 
    try { 
     System.out.println("Enter b value"); 
     b = s.nextInt(); 
     System.out.println("Sum of divison is: " + division(a,b)); 
    } 

    catch(ArithmeticException e) { 
     System.err.println("Dont divide by zero!!!"); 
     success = false; 
    } 
} 
0

在while循環外定義一個布爾值,並將其用於while的條件。

假設我正確理解你的問題,如果用戶的輸入引發異常(即它是無效輸入),並且希望在從用戶獲得有效輸入時跳出循環,則希望保留在循環中。

boolean gotValidInput = false; 
while (!gotValidInput) { 
    try { 

     System.out.println("Enter b value"); 
     b = s.nextInt(); 
     gotValidInput = true; 
     System.out.println("Sum of divison is: " + division(a,b)); 


    } catch(ArithmeticException e) { 
     System.err.println("Dont divide by zero!!!");  
    } catch (java.util.InputMismatchException e) { 
     System.err.println("Enter just a Number!!!"); 
    } finally { 
     System.out.println(); 
    } 
} 

在此實現,你的兩個例外都希望能得到gotValidInput = true;評估之前拋出的,因此它只會被置爲true,如果沒有異常被拋出。

0

你可以把多餘的outter循環,像

while (true) { 
     System.out.println("Enter a value: "); 
     a = s.nextInt(); 
     while(true) { 


     /// terminate the loop in case of problem with a, and allow a user to re done 
     } 
    } 
0

盪滌的警告和移動s到ouside的main方法,並將其定義爲static。看起來s是一個資源泄漏,如果在main中並且從未關閉。

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class whilePerjashtim { 

    private static Scanner s; 

    public static int division(int a, int b) { 

     return a/b; 
    } 

    public static void main(String[] args) { 

     int a, b; 

     s = new Scanner(System.in); 

     System.out.println("Enter a value: "); 
     a = s.nextInt(); 

     boolean input = false; 

     while (input == false) { 

      try { 

       System.out.println("Enter b value"); 
       b = s.nextInt(); 

       System.out.println("Sum of division is: " + division(a, b)); 

       input = true; 
      } 

      catch (ArithmeticException e) { 
       System.err.println("Don't divide by zero!!!"); 
      } 

      catch (InputMismatchException e) { 
       System.err.println("Enter just a Number!!!"); 
      } 

      finally { 
       System.out.println(); 
      } 

     } 

    } 
} 
0

您需要處理錯誤輸入,以及如果你想while循環繼續正常:你必須在每個catch塊結束擺脫錯誤輸入。添加continue將使循環再次運行,直到用戶給出正確的輸入。

catch (InputMismatchException e) 
{ 
    System.err.println("Enter just a Number!!!"); 
    //add this 
    s.nextLine(); 
    continue; 
}