使用扫描仪继续读取数字,直到到达换行符

发布于 2024-10-18 11:30:29 字数 328 浏览 3 评论 0原文

我想从控制台读取几个数字。我想要做到这一点的方法是让用户输入一系列由空格分隔的数字。执行以下操作的代码:

Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
    int i = sc.nextInt();

    //... do stuff with i ...
}

问题是,到达新行时如何停止(同时保留上面易于阅读的代码)?在上面的参数中添加 !hasNextLine() 可以使其立即退出循环。一种解决方案是读取整行并解析出数字,但我认为这有点破坏了 hasNextInt() 方法的目的。

I want to read a couple of numbers from the console. The way I wanna do this is by having the user enter a sequence of numbers separated by a space. Code to do the following:

Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()){
    int i = sc.nextInt();

    //... do stuff with i ...
}

The problem is, how do I stop when reaching a new line (while keeping the easy-to-read code above)? Adding a !hasNextLine() to the argument above makes it quit the loop instantly. One solution would be reading the whole line and the parse out the numbers, but I think the kinda ruins the purpose of the hasNextInt() method.

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

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

发布评论

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

评论(2

浸婚纱 2024-10-25 11:30:29

您希望像这样设置分隔符:

Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator")); 
while (sc.hasNextInt()){
    int i = sc.nextInt();

    //... do stuff with i ...
}

更新:

如果用户输入数字然后按 Enter 键,则上述代码可以正常工作。当在未输入数字的情况下按 Enter 时,程序循环将终止。

如果您需要(更多地将其理解为可用性建议)输入以空格分隔的数字,请查看以下代码:

Scanner sc = new Scanner(System.in);
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
sc.useDelimiter(delimiters); 
while (sc.hasNextInt()){
    int i = sc.nextInt();
    //... do stuff with i ...
    System.out.println("Scanned: "+i);
}

它使用 Pattern 作为分隔符。我将其设置为匹配空格或行分隔符。因此,用户可以输入空格分隔的数字,然后按回车键进行扫描。可以根据需要对任意多行重复此操作。完成后,用户只需再次按 Enter 键,无需输入数字。当输入很多数字时,认为这非常方便。

You want to set the delimiter like this:

Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator")); 
while (sc.hasNextInt()){
    int i = sc.nextInt();

    //... do stuff with i ...
}

UPDATE:

The above code works well if the user inputs a number and then hits enter. When pressing enter without having entered a number the program the loop will terminate.

If you need (understood it more as a suggestion for usability) to enter the numbers space separated, have a look at the following code:

Scanner sc = new Scanner(System.in);
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
sc.useDelimiter(delimiters); 
while (sc.hasNextInt()){
    int i = sc.nextInt();
    //... do stuff with i ...
    System.out.println("Scanned: "+i);
}

It uses a Pattern for the delimiters. I set it to match a white space or a line separator. Thus the user may enter space separated numbers and then hit enter to get them scanned. This may be repeated with as many lines as necessary. When done the user just hits enter again without entering a number. Think this is pretty convenient, when entering many numbers.

旧情别恋 2024-10-25 11:30:29

您可以使用 sc.nextLine() 读取输入行,然后从该字符串构造一个新的扫描器(有一个采用字符串的构造函数重载)并从第二个扫描器读取整数。 (注意:如果这就是您所说的“读取整行并解析出数字”的意思,我将删除这个答案,但看起来您正在谈论使用 Integer.parseInt() 手动读取数字)。

You could use sc.nextLine() to read an input line, and then construct a new Scanner from that String (there is a constructor overload that takes a String) and read the ints from the second Scanner. (Note: If this is what you meant by "reading the whole line and the parse out the numbers", I'll delete this answer, but it looked like you were talking about manually reading the numbers with Integer.parseInt()).

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