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

3

u/BannockBnok Feb 25 '24

Your post doesn't have enough information for us to actually assist you. You've just pasted a small fraction of the code from your IDE without any context or details. "It's not working. What do I do?" is a horrible way to ask others for help. Give us all of your code, and upload it to pastebin with formatting enabled. Then, tell us what you're expecting the code to do and compare it to what it's currently doing.

2

u/Kittensandpuppies14 Feb 25 '24

And tell us what the actual error message is

1

u/Super_Good9981 Feb 25 '24

public class LifeStageIdentifier {

public static void main(String[] args) {

// Create a Scanner object to read input from the keyboard

Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the person's age

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

// Read the input as an integer

int age = scanner.nextInt();

// Determine the person's stage of life based on age

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");

}

// Close the Scanner object

scanner.close();

}

}

1

u/Super_Good9981 Feb 25 '24

I wanted to print terms like Adult when i entered 21 and it just printed 21

1

u/Little_Influence5518 Feb 26 '24

How did you enter 21?