Java:如何使用扫描仪检查输入是整数,并且在指定范围内

发布于 2025-01-21 09:32:58 字数 1466 浏览 0 评论 0原文

我的代码遇到了麻烦。

它应该做什么:

  • 检查扫描仪“ myScanner”是否是整数
  • ,如果它是整数,如果它在1到200之间(包括)问题(包括)

问题:

  • 我需要在第一个猜测后两次输入带有正确值的整数'T正确。

这是我的意思是的屏幕截图:问题的屏幕截图

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    int guess = 0;
    if (myScanner.hasNextInt()) {
        guess = myScanner.nextInt();
    }
    if (1 > guess || guess > 200) {
        while (!(myScanner.hasNextInt()) || !(1 <= guess && guess <= 200)) {
            while (!myScanner.hasNextInt()) {
                myScanner.nextLine();
            }
            guess = myScanner.nextInt();
            if (!(guess >= 1 && guess <= 200)) {
                myScanner.nextLine();
            }
        }
    }
    return guess;
}

我试图应用 @绿巨人的(最高答案)逻辑(至少我认为我确实做到了)(可悲的是对我的项目的要求),并得到了:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    int guess = 0;

    while (!myScanner.hasNextInt() || guess < 1 || guess > 200) {
        while (!myScanner.hasNextInt()) {
            myScanner.nextLine();
        }
        guess = myScanner.nextInt();
        if (guess >= 1 && guess <= 200) {
            break;
        }
    }
    return guess;
}

经过一些测试后,仍然没有任何错误! 如果您仍然发现一个错误,如果您与我分享,我会很高兴!

I'm having trouble with my code.

What it should do:

  • Check if Scanner "myScanner" is an Integer
  • If it is an Integer, if it is between 1 and 200 (both included)

Problem:

  • I need to input an Integer with the correct value twice after the the first guess wasn't correct.

Here is a screenshot of exactly what I mean: Screenshot of the problem

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    int guess = 0;
    if (myScanner.hasNextInt()) {
        guess = myScanner.nextInt();
    }
    if (1 > guess || guess > 200) {
        while (!(myScanner.hasNextInt()) || !(1 <= guess && guess <= 200)) {
            while (!myScanner.hasNextInt()) {
                myScanner.nextLine();
            }
            guess = myScanner.nextInt();
            if (!(guess >= 1 && guess <= 200)) {
                myScanner.nextLine();
            }
        }
    }
    return guess;
}

I have tried to apply @Hulk's (top answer) logic (at least I think I did) into a single method (is sadly a requirement for my project) and got this:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    int guess = 0;

    while (!myScanner.hasNextInt() || guess < 1 || guess > 200) {
        while (!myScanner.hasNextInt()) {
            myScanner.nextLine();
        }
        guess = myScanner.nextInt();
        if (guess >= 1 && guess <= 200) {
            break;
        }
    }
    return guess;
}

After a bit of testing still no error!
If you still find a mistake I would be happy if you shared it with me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2025-01-28 09:32:58

如果将问题分为较小的部分,这会变得更加简单。首先,解决“读取直到我们获得整数”部分并​​将其放入方法中,以便我们可以重复使用它:

private static int readNumber(Scanner sc) {
     while (!sc.hasNextInt()) { 
         sc.nextLine(); // if its not a number, consume the line and wait for new input
     }
     return sc.nextInt(); // always an integer
}

现在,我们只需要添加范围检查:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);             
    
    int n = 0;
    while (n < 1 || n > 200) { // range check, only in one place
        n = readNumber(myScanner); // always returns a number
    }
    return n;       
}

您基本上使事情变得过于复杂,请重复范围检查3以不同方式的位置,很容易因所有这些否定而迷失。保持您的方法简单,一次只做一件事情!


为了提示您的更新问题:如果您绝对需要将它们置于一种方法中,这就是结果:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    
    int n = 0;
    while (n < 1 || n > 200) { // loop until in valid range
        while (!myScanner.hasNextInt()) { 
         myScanner.nextLine(); // if its not a number, consume the line and wait for new input
        }              
        n = myScanner.nextInt(); // always an integer
    }
    return n; // always in valid range
}

请注意,仍然只有一个地方检查了数字输入的检查,而只有一个地方验证了范围。在编程中,几乎没有理由写两次完全相同的东西 - 不要重复自己! - “原理”/指南有时会缩写为干燥。

This gets a lot simpler if you split your problem into smaller parts. First, solve the "read until we got an integer" part and put it into a method so we can reuse it:

private static int readNumber(Scanner sc) {
     while (!sc.hasNextInt()) { 
         sc.nextLine(); // if its not a number, consume the line and wait for new input
     }
     return sc.nextInt(); // always an integer
}

Now, we only need to add the range check:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);             
    
    int n = 0;
    while (n < 1 || n > 200) { // range check, only in one place
        n = readNumber(myScanner); // always returns a number
    }
    return n;       
}

You basically complicated things too much, by repeating the range check in 3 places in different ways, and it's easy to get lost with all these negations. Keep your methods simple, and only do one thing at a time!


To adress your updated question: If you absolutely need to inline these into one method, this is the result:

private static int inputGuess () {
    Scanner myScanner = new Scanner(System.in);
    
    int n = 0;
    while (n < 1 || n > 200) { // loop until in valid range
        while (!myScanner.hasNextInt()) { 
         myScanner.nextLine(); // if its not a number, consume the line and wait for new input
        }              
        n = myScanner.nextInt(); // always an integer
    }
    return n; // always in valid range
}

Note that there is still only one place where there is a check for numeric input, and only one place where the range is validated. In programming, there is rarely a reason to write exactly the same thing twice - don't repeat yourself! - a 'principle'/guideline sometimes abbreviated as DRY.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文