I have typed out a switch statement with three cases and a default. Now, I need
ID: 3717605 • Letter: I
Question
I have typed out a switch statement with three cases and a default. Now, I need to convert the statement to an if-statement using the equal condition and I'm not sure how to do that.
The program is as follows currently:
public class MyClass {
public static void main(String args[]) {
int selection=3;
switch (selection) {
case 1:
System.out.println("Your selection is 1");
break;
case 2:
System.out.println("Your selection is 2");
break;
case 3:
System.out.println("Your selection is 3");
break;
default:
System.out.println("You must select a number from 1 - 3");
break;
}
}
}
I also need to know what the new output print statement would be. Thanks !!
Explanation / Answer
Hear is the code using if else..
===============================================================
public class MyClass
{
public static void main(String args[])
{
int selection=3;
if(selection == 1)
{
System.out.println("Your selection is 1");
}
else if(selection == 2)
{
System.out.println("Your selection is 2");
}
else if(selection == 3)
{
System.out.println("Your selection is 3");
}
else
{
System.out.println("You must select a number from 1 - 3");
}
}
}
================================================================