简体   繁体   English

在while循环中重用扫描器输入以用于多种条件

[英]Reusing scanner input for multiple conditions in while loop

I would like to use a single scanner input as multiple conditions in while loop where are can check if the input is correct or not and if its not correct to re-enter the input value. 我想将单个扫描器输入用作while循环中的多个条件,在where循环中可以检查输入是否正确以及是否不正确以重新输入输入值。

Something like this: 像这样:

while(scannerInput is a int AND scannerInput is > than 0 AND scannerInput is < 10)
{
    //CODE
}

but if I use scanner.hasNextInt() and scanner.nextInt() it prompt me for input instead of using the same one 但是,如果我使用scanner.hasNextInt()和scanner.nextInt(),它将提示我输入而不是使用相同的输入

while (scaner.hasNextInt() && scaner.nextInt() > 0 && scaner.nextInt() < 10)  {

}

Is there a way to store the first input and to re-use it again and again or is there a better solution to my problem ? 有没有办法存储第一个输入并一次又一次地重复使用它,或者有更好的解决方案来解决我的问题?

The call of scanner.nextInt() advances the position at which the scanner reads its input, so the second read may produce a different number, or throw an exception when there is only one int available. 调用scanner.nextInt()提高扫描器读取其输入的位置,因此第二次读取可能会产生不同的数字,或者在只有一个int可用时抛出异常。 To work around this situation you could introduce a variable to hold the value for the second check: 要解决这种情况,您可以引入一个变量来保存第二次检查的值:

int tmp;
while (scaner.hasNextInt() && (tmp = scaner.nextInt()) > 0 && tmp < 10)  {
    ...
}

The second subexpression (tmp = scaner.nextInt()) reads the next integer value from the scanner , and stores it in a temporary variable tmp . 第二(tmp = scaner.nextInt())表达式(tmp = scaner.nextInt())scanner读取下一个整数值,并将其存储在临时变量tmp After that, the value is compared with zero, and finally the same value is compared to 10. 之后,将该值与零进行比较,最后将相同的值与10进行比较。

The assignment is performed before the check of tmp < 10 because parts of && expressions are processed in left to right order. 因为对&&表达式的部分是按从左到右的顺序处理的,所以在检查tmp < 10之前执行分配。

This solution is not ideal, because tmp remains visible after the loop, and because it is not so easy to read. 该解决方案不是理想的,因为tmp在循环后仍然可见,并且因为它不太容易阅读。 You would be better off moving the check inside the loop, like this: 您最好在循环内移动支票,如下所示:

while (scaner.hasNextInt()) {
    int val = scaner.nextInt();
    if (val < 0 || val >= 10) {
        break;
    }
    ...
}

My idea was the code to keep prompting you for new input until its correct 我的想法是代码,不断提示您输入新内容,直到输入正确为止

A common way to achieve this is with a do / while loop: 一种常见的实现方法是使用do / while循环:

int val = -1;
do {
    System.out.print("Enter an int: ");
    if (scanner.hasNextInt()) {
        val = scanner.nextInt();
    } else {
        scanner.nextLine();
    }
} while (val < 0 || val >= 10);

try this code: 试试这个代码:

int s;
while (scanner.hasNext() && (s = scanner.nextInt()) >0 && s <0){
 #code          
}

You have some options here. 您在这里有一些选择。

  1. You could put an if condition inside the while and give while a true as condition.. let the if statement break the while loop 您可以将一个if条件放在while内,然后给while一个true作为条件..让if语句中断while循环
  2. you could write a method which does the checks and give scanner.nextInt() as parameter 您可以编写一个方法进行检查,然后将scanner.nextInt()作为参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM