In the Happy Valley School System, children are classified by age as follows: le
ID: 3648855 • Letter: I
Question
In the Happy Valley School System, children are classified by age as follows:less than 2, ineligible
2, toddler
3-5, early childhood
6-7, young reader
8-10, elementary
11 and 12, middle
13, impossible
14-16, high school
17-18, scholar
greater than 18, ineligible
Given an int variable age , write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age .
Could someone help me with this code to be execute without error?
It gives error message such as "
Problems Detected:
Explanation / Answer
please rate - thanks
you need the entire program in order to compile, I added output for default, and reformatted it so that it's easier to read
I also did a 2nd set of code-slightly "more correct"
import java.util.*;
class main{
public static void main(String args[])
{int age;
Scanner in=new Scanner(System.in);
System.out.print("Enter age: ");
age=in.nextInt();
switch (age)
{case 0:
case 1: System.out.println("ineligible");
break;
case 2: System.out.println("toddler");
break;
case 3:
case 4:
case 5: System.out.println("early childhood");
break;
case 6:
case 7: System.out.println("young reader");
break;
case 8:
case 9:
case 10: System.out.println("elementary");
break;
case 11:
case 12: System.out.println("middle");
break;
case 13: System.out.println("impossible");
break;
case 14:
case 15:
case 16: System.out.println("high school");
break;
case 17:
case 18: System.out.println("scholar");
break;
default: System.out.println("ineligible");
break;
}
}
}
-------------------------------------------------
slightly better version, no case 0 or 1 needed, they go with default
import java.util.*;
class main{
public static void main(String args[])
{int age;
Scanner in=new Scanner(System.in);
System.out.print("Enter age: ");
age=in.nextInt();
switch (age)
{ case 2: System.out.println("toddler");
break;
case 3:
case 4:
case 5: System.out.println("early childhood");
break;
case 6:
case 7: System.out.println("young reader");
break;
case 8:
case 9:
case 10: System.out.println("elementary");
break;
case 11:
case 12: System.out.println("middle");
break;
case 13: System.out.println("impossible");
break;
case 14:
case 15:
case 16: System.out.println("high school");
break;
case 17:
case 18: System.out.println("scholar");
break;
default: System.out.println("ineligible");
break;
}
}
}