Question: Can anyone help me with this?? This code works, but it isn't optimal to read. You might be able to tell that by looking at
Can anyone help me with this??
This code works, but it isn't optimal to read. You might be able to tell that by looking at it) Revise this code to be one (1) if-elseif-else chain that functions the same way as this code does. Do all of these conditions work as intended, or are there bad paths? For example, will 18 actually print "Adult" and 1 print "Baby"? How do you know?
import java.util.Scanner;
public class Week7Problem1
{ public static void main(String[] args)
{ Scanner scan = new Scanner(System.in);
System.out.println("Enter an age: "); int age = scan.nextInt();
if (age < 0){ System.out.println("Not a Valid Age!"); }
else{ if (age < 3){ System.out.println("Baby"); }
else{ if (age < 5) { System.out.println("Toddler"); }
else { if (age < 12){ System.out.println("Child"); }
else { if (age < 18){ System.out.println("Teen"); }
else { if (age >= 18) { System.out.println("Adult"); }
else { System.out.println("Shouldn't Get Here!");
} } } } } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
