110k views
2 votes
Assuming that a user enters 5 as the age, what is the output of the following code snippet? int age = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter your age: "); age = in.nextInt(); if (age < 10) { System.out.println("Kid"); } if (age < 30) { System.out.print("Young"); } if (age < 70) { System.out.print("Aged"); } if (age < 100) { System.out.print("Old"); }

User Viktor K
by
2.9k points

1 Answer

4 votes
Kid
YoungAgedOld

It will print out this way because the code uses if statements, rather than else if statements. As written, every single if statement will be triggered because each expression will evaluate to true. If there were else statements involved, then only the first one that is triggered will print out, and the rest will be ignored.

The reason for the weird formatting/spacing is because only the first print statement is println which adds a new line after printing. The other statements are just print, which does not add a new line or a space.
User Patrickmdnet
by
3.6k points