简体   繁体   English

如何参考用户使用扫描仪输入的最后一个值?

[英]How can I refer to the last value input by the user using Scanner?

I'm using a while loop in order to weed out user input of less than 100 and then make them enter values until they satisfy the condition of being >=100. 我正在使用while循环,以清除小于100的用户输入,然后使它们输入值,直到满足> = 100的条件为止。

But once a value >=100 is entered they would have to enter it again for it to be defined, since the while loop executes getLength once and then updating the int length executes getLength a second time. 但是一旦输入值> = 100,就必须再次输入该值以进行定义,因为while循环执行一次getLength,然后更新int长度再次执行getLength。

while(getLength.nextInt() < 100) //asks user to enter value to check if its less than 100
  {
     System.out.print("You have entered a value less than 100, please enter a value equal to or greater than 100:\n");
  }
int length = getLength.nextInt(); //this makes the user have to enter it again

So instead of outputting: 因此,与其输出:

 25 You have entered a value less than 100, please enter a value equal to or greater than 100: 50 You have entered a value less than 100, please enter a value equal to or greater than 100: 125 //while loop 125 //defining int length 

I want it to have this: 我希望它有这个:

 25 You have entered a value less than 100, please enter a value equal to or greater than 100: 50 You have entered a value less than 100, please enter a value equal to or greater than 100: 125 //prompted by while loop, value is used to define int length 

Essentially, can I get the previously entered user input that was prompted by a while loop? 本质上,我可以获取while循环提示的先前输入的用户输入吗?

You can take an int variable where the user input can be saved. 您可以使用一个int变量,可以在其中保存用户输入。

    int x;

    while((x = getLength.nextInt()) < 100 ) 
    {
         System.out.println("You have entered a value less than 100, please enter a value equal to or greater than 100");
    }

    int length = x;

By this way you can assign the last value entered to length. 通过这种方式,您可以将最后输入的值分配给length。

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

相关问题 如果它不是使用扫描仪的int,如何不允许用户输入? - How can I not allow user input if it is not an int using a scanner? 如何使用Scanner Java重复读取用户输入 - How can I repeatedly read user input using Scanner java 如何在 java 中使用扫描仪仅接受来自用户输入的特定行数 - How can I accept only a specific number of lines from user input using scanner in java 如何从扫描仪库中读取任何用户输入? - How can I read any user input from the scanner library? 如何处理不正确的用户输入扫描仪? - How can I handle incorrect user input into a scanner? 我怎样才能让用户能够通过使用扫描仪功能在键盘上无限输入内容? - How can I allow the user to be able to input something by typing in the keyboard infinitely using the Scanner feature? 如何通过扫描仪输入自定义类别值? - How Can I Input A Custom Class Value Via Scanner? 如何让用户使用java中的扫描仪点击输入默认值? - How can I let a user hit enter for a default value using the Scanner in java? 我如何限制 Java 中的“新扫描仪(System.in)”系统,不进一步处理用户输入数据,除非它是正确的输入值? - How can I restrict the "new Scanner(System.in)" system in Java, to not go further with the user input data, unless it's the correct inputted value? 如何使用扫描仪在用户输入中使用书写器 - How to use writer with user input using scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM