0
How can i take input from scanner ...like i code and "what is your age prints on the screen " and i will put if else condition ?
7 odpowiedzi
+ 1
Take care what you are assigning:
final Scanner scanner =new Scanner(System.in);
final int age = scanner.nextInt();
// now work with age
// afterwards
scanner.close();
+ 3
See scanner is used for taking inputs so it should go like:
Scanner s= new Scanner(System.in);
Now you use scanner to take input and store it in variable:
int age=s.nextInt();
Now that you have the input given by user in variable age, you can print and compare variable age.
+ 3
In your code you have age as a Scanner object and are trying to use it like an integer. Your age.nextInt() gets an integer from the input stream and just returns it to System.out.println() outputting it and never saving its value.
int age =new Scanner(System.in).nextInt();
System.out.println(age);
if (age < 16) {
System.out.println("Too Young");
} else {
System.out.println("Welcome!");
}
+ 1
It supposed to be
Scanner sc = new Scanner(System.in);
//You should ask user to insert their age
System.out.println("what is your age prints on the screen: ");
int age = sc.nextInt();
//Next statement which is your else if condition
0
It should be:
Scanner s = new Scanner(System.in);
and not
System s = new Scanner (System.in);
0
Now it is also giving error