This is an if statement how to convert it to swich statement if (academicYear <=
ID: 3585973 • Letter: T
Question
This is an if statement how to convert it to swich statement
if (academicYear <=5 && academicYear >=1)
{
if (academicYear == 5)
{
System.out.println("I am a " + YEAR5 + " student. ");
}
else if (academicYear == 4)
{
System.out.println("I am a " + YEAR4 + " student. ");
}
else if (academicYear == 3)
{
System.out.println("I am a " + YEAR3 + " student. ");
}
else if (academicYear == 2)
{
System.out.println("I am a " + YEAR2 + " student. ");
}
else if (academicYear == 1)
{
System.out.println("I am a " + YEAR1 + " student. ");
}
else
{
System.out.println(academicYear + " is not a vaild year.");
System.out.println("Please enter a valid academic year.");
System.out.println("Quitting the program.!!!!");
System.exit(0);
}
}
Explanation / Answer
Answer:
switch(academicYear) {
case 5:
System.out.println("I am a " + YEAR5 + " student. ");
break;
case 4:
System.out.println("I am a " + YEAR4 + " student. ");
break;
case 3:
System.out.println("I am a " + YEAR3 + " student. ");
break;
case 2:
System.out.println("I am a " + YEAR2 + " student. ");
break;
case 1:
System.out.println("I am a " + YEAR1 + " student. ");
break;
default:
System.out.println(academicYear + " is not a vaild year.");
System.out.println("Please enter a valid academic year.");
System.out.println("Quitting the program.!!!!");
System.exit(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.