Warm tip: This article is reproduced from serverfault.com, please click

其他-再次使用Java进行循环和用户输入验证

(其他 - Loops and user input validation in java, again)

发布于 2020-11-29 00:39:54

因此,我回想了大约两天,我问了一个有关输入验证的问题,以及如何循环程序直到用户提供有效的输入。

所以我做了一个计算器,我想循环程序的每一步,这样如果用户没有输入double(69,42.0)或适当的运算符char(/,*,+,-),他们将被卡在那一步直到他们弄对了,否则就完全关闭了该应用程序。

因此,我从上一个问题中得到的一件事是,我可以创建一个boolean名为“ restart”值或某种东西,并封装我的整个代码,除了main method显而易见的,最后我可以提出一个问题,他们可以回答truefalse整个问题都可以。应用程序可以再次运行。虽然我承认我的应用上有一个“重新开始”按钮很酷,并且对我的其他所有项目都很有用(可能不是,我确信这样做可以更有效地完成),但它仍然没有使我满意,也没有解决我最初遇到的问题。

所以...

我必须尽我所能(我知道堆栈喜欢证明他们至少尝试过的人)。

例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代码,没有我尝试过的内容和“重新启动”循环。

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.");
    }
}

有一次,我尝试使用相同的“重新启动应用程序”概念,并为代码中的每个步骤都创建了一个布尔变量,说实话这很累人,不值得。

另外,如果这是我所缺少的循环概念,那么我只是一个初学者,所以我很高兴向大家学习。

再次感谢任何回答并帮助我学习的人。

Questioner
Nod
Viewed
11
Robert Bain 2020-11-29 09:32:10

在名为“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);

你可以整理一下,使其适用于其他潜在的错误情况,也许为输出消息创建一个参数并将其作为参数传递。