如何确保Scanner hasNextInt()要求新输入? [英] How do I ensure that Scanner hasNextInt() asks for new input?

查看:194
本文介绍了如何确保Scanner hasNextInt()要求新输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新程序员在这里。这可能是一个非常基本的问题,但它仍然让我感到困惑。

New programmer here. This is probably a really basic question, but it's stumping me nevertheless.

我正在尝试做的是编写一个只提供一个整数输入的方法,这样我就可以使用了在我的主程序中输入,而不必乱用非整数输入。但是,即使用自己的方法编写方法也是有问题的。

What I'm trying to do is write a method that supplies only one integer input so I can use that input in my main program without having to mess around with non-integer inputs. However, even writing the method to do that in its own method seems to be problematic.

public static int goodInput () {
    Scanner input = new Scanner (System.in); //construct scanner
    boolean test = input.hasNextInt(); //set a sentinel value
    while (test == false) { //enter a loop until I actually get an integer
        System.out.println("Integers only please"); //tell user to give me an integer
        test = input.hasNextInt(); //get new input, see if it's an integer
    }
    int finalInput = input.nextInt(); //once i have an integer, set it to a variable
    input.close(); //closing scanner
    return finalInput; //return my integer so I don't have to mess around with hasNextInt over there
}

这似乎在多个级别被打破,但我不确定为什么。

This seems to be broken in multiple levels, but I'm not really sure why.

如果我在第一次输入0或1之类的整数值要求输入,它应该完全跳过循环。但是,相反,它进入循环,并打印仅请整数。更糟糕的是,当我在那里时,它实际上并没有要求输入,只是反复打印该行。

If I enter an integer value like 0 or 1 when I'm first asked for input, it should skip the loop entirely. But, instead, it enters the loop, and prints "Integers only please". Even worse, it doesn't actually ask for input while I'm in there, and just prints that line repeatedly.

我理解后一个问题可能是由于令牌问题,但我不一定确定如何解决它们;关闭然后重新打开扫描程序会让Eclipse错过重复对象,只需将旧输入分配给垃圾字符串变量从未使用过,告诉我在运行时没有找到行,而且我没有足够的经验想想获得新输入的其他方法。

I understand the latter problem is probably due to token issues, but I'm not necessarily sure how to solve them; closing and then reopening the scanner gets Eclipse to bug me over "duplicate objects", simply assigning the old input to a garbage String variable that is never used tells me that "No line was found" at runtime, and I'm not experienced enough to think of other ways to get new input.

即使一旦解决了,我也需要找到一些方法来避免在有整数的情况下进入循环。我真的不明白为什么整数输入在循环之间开始,所以我不确定这是怎么可能的。

Even once that's solved, I need to find some way to avoid entering the loop in the case of having an integer. I don't really understand why integer inputs inter the loop to begin with, so I'm not sure how this would be possible.

请帮忙?对不起,如果这是一个老问题;试过看过去的问题,但似乎没有一个问题与我有相同的问题。

Please help? Sorry if this is an old question; tried looking at past questions but none of them seem to have the same problem that I have.

推荐答案

你很接近:这是有效的对我来说很好:

You were close: this works fine for me:

Scanner input = new Scanner(System.in); //construct scanner
while(!input.hasNextInt()) {
    input.next(); // next input is not an int, so consume it and move on
}
int finalInput = input.nextInt();
input.close(); //closing scanner
System.out.println("finalInput: " + finalInput);

通过调用 input.next()进入你的while循环,你使用非整数内容并再次尝试,直到下一个输入是一个int。

By calling input.next() in your while loop, you consume the non-integer content and try again, and again, until the next input is an int.

这篇关于如何确保Scanner hasNextInt()要求新输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆