2017-07-27 95 views
0

程序要求用戶輸入雙數字1和雙數字2 ,如果有例外,我希望它再次詢問數字1和數字2的輸入嘗試在do while while循環中捕獲

public static void main (String[] args) { 
    Scanner sc = new Scanner(System.in); 
    double num1, num2; 
    int error = 0; 
    int text; 
    System.out.print("Enter 4 "); 
     text = sc.nextInt(); 

    do{ 

     try{ 

    if(text == 4){ 
      System.out.print("Enter number 1: "); 
      num1 = sc.nextDouble(); 
      System.out.print("Enter number 2: "); 
      num2 = sc.nextDouble(); 
      double quotient = num1/num2; 
      System.out.println("The Quotient of "+num1 + "/" +num2+ " = "+quotient); 
      } 
     }catch(Exception ex){ 
      System.out.println("You've entered wrong input"); 
       error = 1; 
     }         

      }while(error == 1); 
} 

然後當我嘗試代碼,如果它會通過在NUM1或num輸入查詢字符串捕獲異常2我在這個無限循環: Enter number 1: You've entered wrong input Enter number 1: You've entered wrong input Enter number 1: You've entered wrong input Enter number 1: You've entered wrong input Enter number 1: You've entered wrong input

+3

請提供[mcve](注意「完整」位)。 – assylias

+0

你從哪裏獲得雙倍? – Shirkam

+0

通過聲明和(重新)使用從用戶獲取輸入的單獨方法,直到用戶輸入有效數據爲止,您的代碼將受益。 – Bohemian

回答

3

您需要的error變量重新循環內

do { 
    error = 0; 
    //... 
} while(error == 1); 
+0

仍然有循環 – Pon

+0

@Pon請張貼您嘗試過的代碼。 – Guy

+0

@Pon您需要在'do while'循環中添加'error = 0;'。 – Guy

0

你可能想測試,如果沒有錯誤:

}while(error != 1); 

}while(error == 0); 
0

你所需要的自稱,如果輸入的是無效的輸入方法。

double getInput(Scanner sc) { 
    try { 
     double num = sc.nextDouble(); 
     return num; 
    } catch(Exception ex) { 
     System.out.println("You've entered wrong input"); 
     return getInput(sc); 
    }  
} 

並用另一種方法調用這個方法兩次。

0

它可能看起來很醜陋,但這裏是一個辦法做到這一點

do 
    { 
    if(...) 
    { 

     boolean successReading = false; 
     while(!successReading) 
     { 
     try 
     { 
      System.out.print("Enter number 1: "); 
      num1 = sc.nextDouble(); 
      System.out.print("Enter number 2: "); 
      num2 = sc.nextDouble(); 

      successReading = true; 

      double product = num1*num2; 
     } 
     catch(Exception e) 
     { 
      successReading = false; 
     } 
     } 

    } 
    }while(...) 
+0

嘿這是一個noob問題,但最後一次是什麼? – Pon

+0

您的條件,以保持你的'做while'循環。但讓我問你一個問題,你爲什麼寫這個'if(text == 4){' – Ali

1

它在C#中,但比較相似:)

public class Program 
{ 
    private static double ReadUserInput (string message) 
    { 
    // This is a double 
    // The '?' makes it nullable which is easier to work with 
    double? input = null; 

    do 
    { 
     // Write message out 
     Console.Write(message); 
     // Read answer 
     var inputString = Console.ReadLine(); 
     // Temp variable for the number 
     double outputNumber = 0; 
     // Try parse the number 
     if (double.TryParse(inputString, out outputNumber)) 
     { 
     // The number was parsable as a double so lets set the input variable 
     input = outputNumber; 
     } 
     else 
     { 
     // Tell the user the number was invalid 
     Console.WriteLine("Sorry bud, but '" + inputString + "' is not a valid double"); 
     } 
    } 
    while (input == null); // Keep running until the input variable is actually set by the above 

    // Return the output 
    return (double)input; 
    } 

    public static void Main() 
    { 
    // Read a number 
    var num1 = ReadUserInput("Enter number 1:"); 
    // Read another number 
    var num2 = ReadUserInput("Enter number 2:"); 
    // Show the calculation 
    Console.WriteLine("Answer: " + (num1*num2)); 
    } 
} 

Demo

而對於實際的代碼(在JAVA中):

public class JavaFiddle 
{ 
    public static void main (String[] args) 
    { 
    // Read a number 
    Double num1 = ReadUserInput("Enter number 1:"); 
    // Read another number 
    Double num2 = ReadUserInput("Enter number 2:"); 
    // Show the calculation 
    System.out.println("Answer: " + (num1*num2)); 
    } 

    public static Double ReadUserInput (String message) 
    { 
    java.util.Scanner inputScanner = new java.util.Scanner(System.in); 
    Double input = null; 

    do 
    { 
     // Write message out 
     System.out.println(message); 
     // Read answer 
     String inputString = inputScanner.nextLine(); 
     try 
     { 
     // Try parse the number 
     input = Double.parseDouble(inputString); 
     } 
     catch (NumberFormatException e) 
     { 
     // Tell the user the number was invalid 
     System.out.println("Sorry bud, but '" + inputString + "' is not a valid double"); 
     } 
    } 
    while (input == null); // Keep running until the input variable is actually set by the above 

    // Return the output 
    return input; 
    } 
} 
+0

有沒有問題的'C#'標籤。爲什麼你決定發佈無益的答案? – talex

+0

它看起來像一個很好的解決方案,但你應該寫在'java'中 – Ali

+0

它更多的過程,以及如何用OOP風格編寫這個過程,以及過程運行的方式是我在回答問題而不是代碼。 JAVA和C#沒有太大的區別,但是因爲我在很久以前沒有做過JAVA,所以我寧願發佈工作代碼,而不是那些可能工作的東西,或者如果它沒有,就會迷惑某人 - 這更糟糕。 我會看看在JAVA重寫更符合Q :) –

1

沒有必要利用異常處理。只需使用Scanner.hasNextDouble()方法來確定實際用戶輸入是否爲雙倍,否則繼續循環。

package com.company; 

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     double num1, num2; 
     num1 = readDouble(1, sc); 
     num2 = readDouble(2, sc); 
     double quotient = num1/num2; 
     System.out.println("The Quotient of " + num1 + "/" + num2 + " = " + quotient); 
    } 

    private static double readDouble(int i, Scanner sc) { 
     while (true) { 
      System.out.print("Enter number " + i + ": "); 
      if (!sc.hasNextDouble()) { 
       System.out.println("You've entered wrong input"); 
       sc.next(); 
       continue; 
      } 
      break; 
     } 
     return sc.nextDouble(); 
    } 
} 
+0

抱歉沒有把它放在代碼中,但有一個還有多個如果有的話,因爲我希望用戶定義他/她想要做什麼的方法。 我試着通過添加布爾繼續和打破它註釋的代碼沒有任何錯誤,但循環不開始時,我輸入字符串,而不是雙重 – Pon

+0

我錯過了'sc.next();' –

+0

修復它。 – Pon

0

您需要添加sc.next();catch塊。

nextDouble如果發生異常,方法不會清除緩衝區。所以下次你調用它時,你會得到相同的錯誤,因爲舊的輸入仍然在緩衝區中。

此外,您還需要在循環開始時重置error標誌。

+0

嘿它現在似乎工作謝謝:D – Pon

+0

然後標記爲接受。它有助於爲那些和你有同樣問題的人找到正確的答案。 – talex