how to add two values in switch cases java code example
Example 1: java switch multiple values
switch (num) {
case 5:
case 2:
case 3:
System.out.println("5,2,3");
break;
case 4:
case 6:
System.out.println("4,6");
break;
case 1:
System.out.println("1");
break;
default:
System.out.println("Inavlid number");
break;
}
Example 2: Java switch with multiple cases
public class SwitchMultipleCase
{
public static void main(String[] args)
{
System.out.println(multipleCase("one"));
System.out.println(multipleCase("two"));
}
static String multipleCase(final String number)
{
String str = null;
switch(number)
{
case("one"):
case("three"):
case("five"):
str = "odd numbers";
break;
case("two"):
case("four"):
case("six"):
str = "even numbers";
break;
default:
str = "please enter valid input";
}
return str;
}
}