2017-07-27 79 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