r/learnjava • • Feb 25 '24

Java if else statement

System.out.print("40: ");

int age = scanner.nextInt();

if (age < 0 || age > 120) {

System.out.println("Invalid age");

} else if (age <= 12) {

System.out.println("Child");

} else if (age <= 19) {

System.out.println("Teenager");

} else if (age <= 64) {

System.out.println("Adult");

} else {

System.out.println("Senior Citizen");

}

I wanted to print a specific result but it wasn't coming up, what errors am i making

0 Upvotes

25 comments sorted by

View all comments

Show parent comments

-2

u/Super_Good9981 Feb 25 '24

System.out.print("121: ");

int age = scanner.nextInt();

4

u/anonymz007 Feb 25 '24

Import java.util.Scanner;

Scanner scanner = new Scanner(System.in);

I added this... And the code worked ... It takes the next integer value entered in the CLI.

-1

u/Super_Good9981 Feb 25 '24

I wanted to enter values to get terms like child, adult, or other terms.

2

u/[deleted] Feb 25 '24

You have to do both. You create a scanner like poster above Said . You want that Scanner scanner = new Scanner() at top of your code and then thr scanner.nextInt() in your code is what's actually using it