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.
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.
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?
You can take an int variable where the user input can be saved.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.