Java - 持续检查输入是否为双精度

Java - Continuously check if input is a double

所以,我一直对我正在研究的这个方法有点困惑。基本上,我需要接受输入的方法,检查它是否真的是一个数字,然后看看它是否在最大值和最小值之间。

我目前遇到的问题是当我要求用户输入一个新数字时,因为他们的输入在 0 之前或大于 1,000,000。当用户输入数字以外的任何内容时,它会崩溃。有什么想法吗?

如果您决定向我展示如何使用 Try/Catch 语句修复它;你能告诉我它是如何工作的吗?我还不是 100% 确定它们是如何工作的!

代码已修改为最新版本

public static double inRange( Scanner scan, int min, int max ) {
    double Amount = 0.0;
    boolean isValid = false;

    while( isValid == false ) {
        if( scan.hasNextDouble() ) {
            Amount = scan.nextDouble();
            if( Amount > max ) {
                System.out.print( "Error: You cannot enter a number greater than ,000,000. Try again: " );
            }
            else if( Amount < min ) {
                System.out.print( "Error: You cannot enter a negative number. Try again: " );
            }
            else {
                isValid = true;
                break;
            }
        }
        else {
            System.out.print( "Error: You must enter a number! Try again: " );
            scan.next();
        }
    }
    return Amount;
}

我认为错误在这些行中

if( scan.hasNextDouble() ) {
    ...
} else {
    System.out.print( "Error: You must enter a number! Try again: " );
    Amount = scan.nextDouble();  // problem is here, remove it!
}

如果扫描没有下一个double,如何获取下一个double值?
此外,我认为这一行

Amount = scan.nextDouble();

应该改成这个

scan.next();

这些是我的代码,它们工作正常。

Scanner scan = new Scanner(System.in);
int min = 0;
int max = 1000000;
double Amount = 0.0;
boolean isValid = false;

while (isValid == false) {
    if (scan.hasNextDouble()) {
        Amount = scan.nextDouble();
        if (Amount > max) {
            System.out.print("Error: You cannot enter a number greater than ,000,000. Try again: ");
        }
        else if (Amount < min) {
            System.out.print("Error: You cannot enter a negative number. Try again: ");
        }
        else {
            isValid = true;
                }
        }
    else {
        System.out.print("Error: You must enter a number! Try again: ");
        scan.next();
    }
}

好吧,有几种方法可以解决这个问题,还有一些方法可以改进您的代码。

所以问题是扫描仪 returns 一个字符串,您将该字符串分配给一个双精度值,当它类似于“23”或“2.4”但不适用于 "Hello"。 你很好地检查了字符串是否在开头有一个double

  if( scan.hasNextDouble() )

但你不检查它是否在 else

 else {
        System.out.print( "Error: You must enter a number! Try again: " );
        Amount = scan.nextDouble();
    }

所以如果用户输入 String 比如 "Hello" 它说扫描器 scan 没有 double 然后告诉用户但是分配 Amount 等于下一个输入的东西而不检查它是否是双精度数。所以如果你再次输入 "Hello" 它会崩溃。但是如果你像有人建议的那样删除那条线你会得到一个无限循环因为如果你看 the documentation for hasNextDouble 它说 "The scanner does not advance past any input." 这意味着它会继续检查同样的事情你输入,除非您移动扫描仪。因此,您需要做一些事情来移动扫描仪,例如 scan.nextLine() 刚刚输入的 returns 字符串,您可以使用它来为用户提供更多信息,例如 System.out.print( "Error: You must enter a number! " + scan.nextLine() +" is not a number. Try again: " );

所以如果你输入 "Hello" 它会告诉你你好不是数字。

此外,naming convention 是变量的命名以小写字母开头,因此使用 amount 而不是 Amount。您还可以使代码更加模块化,因为现在虽然最小值和最大值是可以更改的变量,但您总是告诉用户它们是 1,000,000 和 0。因此,如果我将最大值设为 2,然后在程序中输入 3会告诉我

"Error: You cannot enter a number greater than ,000,000"

如果将该行更改为 System.out.print( "Error: You cannot enter a number greater than $" + max + ". Try again: " ); 它会更有意义。

是的,您可以通过使用 try catch 来捕获 InputMismatchException 来解决原始问题,但是这个答案已经很长了,您可以阅读 here.

我最终解决了这个问题。我在调用我的 inRange() 方法的方法中有一个 'scan.next();' 。因此,一旦我删除了它要求输入两次的问题就解决了,并且该方法成功地检查它是否实际上是一个双精度数。

    public static double deposit(double bankAcc, String pinCode, Scanner scan ) {
    boolean valid = false;
    double depositAmount = 0;

    System.out.print( "Please enter your PIN #: " );
    String pinCodeTest = scan.nextLine();

    while( valid == false ) {
        if( pinCodeTest.equals(pinCode)) {
            System.out.print( "How much would you like to deposit?: " );
            depositAmount = inRange( scan, 0, 1000000 );
            valid = true;
        }
        else {
            System.out.println( "Error! Incorrect PIN #. Try again: " );
            pinCodeTest = scan.nextLine();
        }
// I had a 'scan.next();' right here... Thinking it acted as a buffer clear!
    }
    return depositAmount;
}
    public static double inRange( Scanner scan, int min, int max ) {
    double amount = 0.0;
    boolean isValid = false;

    while( isValid == false ) {
        if( scan.hasNextDouble() ) {
            amount = scan.nextDouble();
            if( amount > max ) {
                System.out.print( "Error: You cannot enter a number greater than ,000,000. Try again: " );
            }
            else if( amount < min ) {
                System.out.print( "Error: You cannot enter a negative number. Try again: " );
            }
            else {
                isValid = true;
                break;
            }
        }
        else {
            System.out.print( "Error: You must enter a number! Try again: " );
            scan.next();
        }
    }
    return amount;
}