Loop

java

#1

I rote the program: The question is. I need when somebody input number 120 <int==0 the program wrote "What is your age?" till the right (not 0 and not more that 120) number input and it go on program. I tried to use while, but it do endless loop

import java.util.Scanner;

public class Java2 {
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
		System.out.println("What is you name? ");
		String line = input.nextLine();
		System.out.println("Your name is " + line);
		System.out.println("What is your age? ");
		int value = input.nextInt();
	//while(value==0||value>120){
	//System.out.println("What is your age? ");}	//char answerCoice = 'value';
		if (value<16){
		System.out.println("U can't drive");
		}
	else{
		System.out.println("U can drive");
	
	}

}
	
}

#2

It's the perfect use case for the while loop. But you need to change value of value in the block of the while (to avoid infinite loop) -> https://repl.it/CoXR/0


#3

Thank U very much. it means the string "value = input.nextInt();" give me opportune to input a new value. and go on


#4

Yes, exactly! :slight_smile:

You are very welcome.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.