如果同一行上的多个输入无效,则使用while循环显示错误消息?

时间:2017-10-20 17:15:31

标签: java while-loop

目前我正在研究一种简单的代数公式计算器,以便在某些Java基础知识方面做得更好,但我遇到了一个问题。

我的一种方法是二次方程式。它要求用户在同一行输入A,B和C,然后程序执行计算。但是当我做一个while循环来检查输入是否为double时,当我输入一个不正确的值时(例如,3 5 x),我会收到错误。

我是否需要为每个单独的变量执行循环,或者是否有更快的方法来检查一个输入中的所有3个变量是否可接受?

public static void quadraticFormula() {
    Scanner sc = new Scanner(System.in);

    System.out.print("Please enter the values(in order) for A, B and C: ");

    while(!sc.hasNextDouble()) {
        System.out.println("Error! One or more of your inputs are not acceptable. Please try again: ");
        sc.nextDouble();
    }

    double a = sc.nextDouble();
    double b = sc.nextDouble();
    double c = sc.nextDouble();

    int discriminant = (int) (Math.pow(b, 2) - 4 * a * c);

    double rootPlus = (-1 * b + Math.sqrt(discriminant)) / (2 * a);
    double rootMinus = (-1 * b - Math.sqrt(discriminant)) / (2 * a);

    System.out.println("Your answers are as follows: ");
    System.out.println("\tB + : " + rootPlus);
    System.out.println("\tB - : " + rootMinus);
}

3 个答案:

答案 0 :(得分:0)

您发布的代码尝试读取6个值,循环中有3个,之后有3个。

我会将值读入字符串,然后在try catch块内解析它们(每个变量一个)。通过这种方式,您可以验证输入是否代表双精度,并且您可以进行计算。

答案 1 :(得分:0)

试试这个。我使用布尔标志和解析来测试double。 它的计算不知道它们是否正确,但输入的问题是正确的。



    import java.util.Scanner;

    public class Teste {

        public Teste() {

            Scanner sc = new Scanner(System.in);
            String[] input = null;
            boolean flag = true;
            double a = 0;
            double b = 0;
            double c = 0;

            while (flag) {
                System.out.print("Please enter the values(in order) for A, B and C: ");
                String str = sc.nextLine();
                input = str.split(" ");
                System.out.println("You entered: " + str);
                if (input.length == 3) {
                    try {
                        a = Double.parseDouble(input[0].trim());
                        b = Double.parseDouble(input[1].trim());
                        c = Double.parseDouble(input[2].trim());
                        flag = false;
                    } catch (NumberFormatException e) {
                        flag = true;
                        System.out.println("Input an integer");
                    }
                } else {
                    flag = true;
                    System.out.print("Please enter the values(in order) for A, B and C: ");
                }
            }
            sc.close();

            int discriminant = (int) (Math.pow(b, 2) - 4 * a * c);
            System.out.println(discriminant);
            double rootPlus = (-1 * b + Math.sqrt(discriminant)) / (2 * a);
            double rootMinus = (-1 * b - Math.sqrt(discriminant)) / (2 * a);

            System.out.println("Your answers are as follows: ");
            System.out.println("\tB + : " + rootPlus);
            System.out.println("\tB - : " + rootMinus);
        }

        public static void main(String[] args) {
            Teste app = new Teste();
        }
    }

答案 2 :(得分:0)

我对你的问题有一些想法。我认为你只能在扫描仪上迭代一次,所以你想要获得它们所需的值。在获取输入时,您可以检查以确保它是双倍的。如果没有,请在try / catch中抛出一个受控错误,然后再次提示用户。

您已经知道需要正好3个双打,因此创建一个列表来存储您的a,b,c可能是一个很好的选择,其中索引0 = a,索引1 = b和索引2 = c。我创建了一个可能有助于使用此策略的方法:

public static void scannerPractice() {

        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter the values(in order) for A, B and C:    
             ");

        //create list to store your a, b, c
        ArrayList<Double> doubles = new ArrayList<Double>();

        //iterate over doubles until it has 3 elements (a, b, c)
        while (doubles.size() < 3) {
            //You can use a try to make sure the user is entering doubles
            try {
                doubles.add(sc.nextDouble());
              //found a mismatch, so print error message, enter method to try  
              //try again and break
            } catch (InputMismatchException e) {
                System.out.println("Not a double. Please try again: ");
                scannerPractice();
                break;
            }
        }
        //list now has a, b, c
        if (doubles.size() == 3) {
            //iterate over the list to make sure you're getting what's
            //expected
            for (int i = 0; i < doubles.size(); i++) {
                System.out.println(doubles.get(i));

            }
            //enter math logic here
        }
        //good practice to close scanner when through
        sc.close();
    }