【问题标题】:Why do I have to enter in the input twice before my method accepts the input? [duplicate]为什么在我的方法接受输入之前必须输入两次? [复制]
【发布时间】:2023-01-08 13:44:42
【问题描述】:

我正在制作一个简单的抵押贷款计算器,并在继续使用下一个方法之前尝试通过两个“if 语句”验证两件事。我要检查的第一件事是扫描仪的输入是否为整数。如果是,那么我想检查整数是否在 1,000 到 1,000,000 之间。

下面是具体代码:

public static Integer checkPrincipalValidation(Scanner scanner) {
        while (true) {
            if (scanner.hasNextInt()) {
                principal = parseInt(scanner.nextLine());
                if (principal >= 1_000 && principal <= 1_000_000) {
                    break;
                }
                    System.out.println(scanner.nextLine() + " is not between 1,000 and 1,000,000. Please enter correct Principal (1K - $1M):");
            }
            if (!scanner.hasNextInt()) {
                System.out.println(scanner.nextLine() + " is not a valid integer. Please enter correct Principal (Integer):");

            }
        }
        return principal;
    }

如果有兴趣,下面是整个文件:


import java.util.Scanner;

import static java.lang.Float.parseFloat;
import static java.lang.Integer.parseInt;

public class Validation {
    static int principal;

    public static Integer checkPrincipalValidation(Scanner scanner) {
        while (true) {
            if (scanner.hasNextInt()) {
                principal = parseInt(scanner.nextLine());
                if (principal >= 1_000 && principal <= 1_000_000) {
                    break;
                }
                    System.out.println(scanner.nextLine() + " is not between 1,000 and 1,000,000. Please enter correct Principal (1K - $1M):");
            }
            if (!scanner.hasNextInt()) {
                System.out.println(scanner.nextLine() + " is not a valid integer. Please enter correct Principal (Integer):");

            }
        }
        return principal;
    }


    public static Float checkInterestValidation(Scanner scanner) {
        while (true) {
            if (scanner.hasNextFloat() || scanner.hasNextInt()) {
                if (scanner.hasNextInt()) {
                    return parseFloat(scanner.nextLine());
                }
                return scanner.nextFloat();
            } else {
                System.out.println(scanner.nextLine() + " is not a valid rate");
                System.out.print("Please enter correct Rate: ");
            }
        }

    }

    public static Integer checkPeriodValidation(Scanner scanner) {

        while (true) {
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                System.out.println(scanner.nextLine() + " is not a valid period");
                System.out.print("Please enter correct Period (Years): ");
            }
        }
    }


}

当它通过第一个“if 语句”时,我必须在进入第二个“if 语句”之前输入两次数字。为什么?感谢您的时间。我没有编码一年,所以我非常生疏,而且对 Java 仍然非常陌生,哈哈!

【问题讨论】:

标签: java if-statement while-loop


【解决方案1】:

每次拨打scanner.nextLine()时,您都必须提供额外的输入。

让我将 cmets 写入您的方法以向您展示要点:

public static Integer checkPrincipalValidation(Scanner scanner) {
    while (true) {
        if (scanner.hasNextInt()) {  // (1)
            principal = parseInt(scanner.nextLine());  // (2)
            if (principal >= 1_000 && principal <= 1_000_000) {
                break;
            }
            //  (3) is the next line
            System.out.println(scanner.nextLine() + " is not between 1,000 and 1,000,000. Please enter correct Principal (1K - $1M):");
        }
        if (!scanner.hasNextInt()) {  // (4)
            //  (5) is the next line
            System.out.println(scanner.nextLine() + " is not a valid integer. Please enter correct Principal (Integer):");
        }
    }
    return principal;
}

在标记为 (1) 的代码行上,您检查输入是否包含有效的整数值。

如果是,则在代码行 (2) 处读取该值,对其进行解析并将该值存储在 principal 中。

然后,如果本金的值超出有效范围,则读取代码行 (3) 处的下一行输入。这与您在代码行 (2) 中读取的值不同 - 该值已被消耗且无法再次检索!

然后在代码行 (4) 处再次检查输入的下一个值是否为有效整数值。如果没有有效的整数值,您将在代码行 (5) 处使用该输入。

总的效果是这样的:

例如,如果用户输入值“1”,则该值通过代码行 (1) 上的条件,从输入中读取并在代码行 (2) 中解析为 int 值。

由于这个值不在 1000 <= 值 <= 1_000_000 的有效范围内,代码继续到第 (3) 行,您需要用户输入额外的一行,以便您可以告诉他这个新输入不在 1,000 和1,000,000

然后代码前进到代码行 (4),期待额外的输入。

要解决这个问题,您应该像这样重写代码:

public static int checkPrincipalValidation(Scanner scanner) {
    while (true) {
        if (scanner.hasNextInt()) {
            principal = scanner.nextInt();
            if (principal >= 1_000 && principal <= 1_000_000) {
                break;
            }
            System.out.println(principal + " is not between 1,000 and 1,000,000. Please enter correct Principal (1K - $1M):");
        } else {
            System.out.println(scanner.nextLine() + " is not a valid integer. Please enter correct Principal (Integer):");
        }
    }
    return principal;
}

请注意,虽然此代码解决了您眼前的问题,但您稍后可能会在使用 scanner.nextLine() 阅读以下输入时遇到其他问题 - 有关更多信息,请参阅 Scanner is skipping nextLine() after using next() or nextFoo() 的答案。

该问题和答案的要点:如果您的输入包含混合输入(一方面是数字或单个单词等单个标记,另一方面是完整的输入行),则使用 Scanner 非常困难。

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多