同样,java中的循环和用户输入验证

m528fe3b  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(380)

所以,我想两天前我问了一个关于输入验证的问题,以及如何循环程序直到用户给出有效的输入。。
所以我做了一个计算器,我想循环程序的每一步,这样如果用户没有输入 double (69,42.0)或适当的操作员 char (/,*,+,-)他们将被卡在那一步,直到他们得到它的权利或关闭应用程序完全。
所以我从最后一个问题中得到的一点是我可以 boolean 名为“restart”之类的值,并封装了除
main method 很明显,最后我可以提出一个他们可以回答的问题 true 或者 false 整个应用程序可以再次运行。虽然我承认在我的应用程序上有一个“重新开始”按钮是很酷的,而且对我其他所有的a项目都很有用(可能不是,我相信这可以更有效地完成),但它仍然没有满足我和我最初的问题。
所以。。。
我要尽我所能去尝试(我知道斯塔克喜欢那些表现出至少尝试过的人)。
例1

Scanner input = new Scanner(System.in);

        double valX;

            System.out.println("Calculator Activated.");

                do
                {
                    System.out.print("Value X: ");
                    valX = input.nextDouble();
                }while (!input.hasNextDouble());
                {
                    System.out.println("Invalid number!");
                }
//Didn't work ¯\_(ツ)_/¯

例2

Scanner input = new Scanner(System.in);

        double valX;

            System.out.println("Calculator Activated.");

            while (!input.hasNextDouble())
            {
                System.out.println("Value X: ");
                valX = input.nextDouble();
            }
//neither this one...

请注意,你们给我的解决方案必须适用于程序的每一步。
更清楚一点的是,整个src代码没有我尝试过的东西和“restart”循环。

import java.util.Scanner;

public class CalTest
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        double valX, valY, divided, multiplied, added, subtracted;  //Declared all my variables...
        char operator;
        boolean restart; //Didn't need to declare it true or false since I'm using a Scanner anyway

        do //Start of the entire program
        {
            System.out.println("Calculator Activated.");

            System.out.print("Value X: "); //I need a loop here...
            valX = input.nextDouble();

            System.out.print("Operator: "); //And here...
            operator = input.next().charAt(0);

            System.out.print("Value Y: "); //And here too..
            valY = input.nextDouble();

            divided = valX / valY;
            multiplied = valX * valY;
            added = valX + valY;
            subtracted = valX - valY;

            if (operator == '/')
                System.out.println("Result: " + divided );
            else if (operator == '*')
                System.out.println("Result: " + multiplied); //<--Not sure if I need a loop with the if's
            else if (operator == '+')
                System.out.println("Result: " + added);
            else if (operator == '-')
                System.out.println("Result: " + subtracted);
            else
                System.out.println("Invalid operator!");

            System.out.print("Try again? "); //I also need a loop here, I think.
            restart = input.nextBoolean();

        } while (restart); //End of it if you declared false.

        System.out.println("Calculator terminated.");
    }
}

有一次,我尝试使用相同的“重启应用程序”概念,并为代码中的每一步都创建了一个布尔变量,说实话,这很烦人,也不值得。
另外,我只是一个初学者,如果这是一个循环的概念,我错过了那么我很高兴向你们学习。
再次感谢所有回答并帮助我学习的人。

tmb3ates

tmb3ates1#

在类中的最后一个代码示例中 CalTest 分配的位置 valX = input.nextDouble(); 您可以调用一个递归方法来处理异常,直到输入是您想要的。像这样:

private static double getNextDouble(Scanner input) {
    try {
        return input.nextDouble();
    } catch (InputMismatchException e) {
        System.out.println("Value X must be a number: ");
        input.next();
        return getNextDouble(input);
    }
}

你将取代 valX = input.nextDouble();valX = getNextDouble(input); .
您可以整理它并使其适用于其他潜在的错误情况,也许可以为输出消息创建一个参数并将其作为参数传入。

ee7vknir

ee7vknir2#

public static void main(String[] args) {
    double valX=0,valY=0;
    char operator='0';//dummy default

    Scanner input = new Scanner(System.in);
    System.out.println("Calculator Activated.");

    Double dX = null;
    do
    {
        System.out.print("Value X: ");
        dX = getDouble(input.next());
    }while (dX==null);
    {
        valX = dX;
    }

    Character op = null;
    do
    {
        System.out.print("Operator: ");
        op = getOperator(input.next());
    }while (op==null);
    {
        operator=op;
    }

    Double dY = null;
    do
    {
        System.out.print("Value Y: ");
        dY = getDouble(input.next());
    }while (dY==null);
    {
        valY = dY;
    }

    System.out.println("Done: "+ valX + " "+operator + " " +valY);
}

static Double getDouble(String input) {
    Double d = null;
    try {
        d = new Double(input);
    }catch(NumberFormatException ex){
    }
    return d;
}

static Character getOperator(String input) {
    Character c = null;
    if("+".equals(input) || "-".equals(input) || "*".equals(input) || "/".equals(input)){
        c = input.charAt(0);
    }
    return c;
}

相关问题