Java - Using Two Values For One Switch Case Statement - Stack Overflow PDF
Java - Using Two Values For One Switch Case Statement - Stack Overflow PDF
Make your voice heard. Take the 2019 Developer Survey now
Using two values for one switch case statement Ask Question
switch (name) {
case text1: {
//blah
35 break;
}
case text2: {
//blah
break;
}
case text3: {
//blah
break;
}
case text4: {
//blah
break;
}
case text1||text4: {
//blah
break;
}
java switch-statement
10 Answers
class SwitchDemo {
public static void main(String[]
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0)
!(year % 100 ==
|| (year % 400 =
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("I
break;
}
System.out.println("Number of
+ numDays)
}
}
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of ServiceThis
. is the output from the code:
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 2/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
Number of Days = 29
FALLTHROUGH:
EXAMPLE CODE:
int month = 8;
switch (month) {
case 1: futureMonths.add
case 2: futureMonths.add
case 3: futureMonths.add
case 4: futureMonths.add
case 5: futureMonths.add
case 6: futureMonths.add
case 7: futureMonths.add
case 8: futureMonths.add
case 9: futureMonths.add
case 10: futureMonths.add
case 11: futureMonths.add
case 12: futureMonths.add
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Inval
} else {
for (String monthName : f
System.out.println(mon
}
}
}
}
August
September
October
November
December
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 3/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase())
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
int returnedMonthNumber =
StringSwitchDemo.getMonth
if (returnedMonthNumber == 0)
System.out.println("Inval
} else {
System.out.println(return
}
}
}
By using our site,FROM Java Docsthat you have read and understand our Cookie Policy, Privacy Policy, and our
you acknowledge
Terms of Service.
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 4/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
edited Feb 10 '14 at 6:44
Dennis Meng
4,678 9 27 35
case text1:
32 case text4: {
//blah
break;
}
case text1:
By using our site,case
you acknowledge
text4: that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service. //blah
break;
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 5/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
Just do
switch (name) {
case text1: {
method1();
break;
}
case text2: {
method2();
break;
}
case text3: {
method3();
break;
}
case text4: {
method1();
break;
}
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 6/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 7/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
switch (month) {
case 1, 3, 5, 7, 8, 10, 12
numDays = 31;
}
case 4, 6, 9, 11 ->{
numDays = 30;
}
case 2 ->{
if (((year % 4 == 0) &
!(year % 100 =
|| (year % 400
numDays = 29;
else
numDays = 28;
}
default ->{
System.out.println("In
}
}
System.out.println("Number of
}
}
case text1:
case text4: {
//Do something
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
break;
Terms of Service.
}
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 8/9
9/2/2019 java - Using two values for one switch case statement - Stack Overflow
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
Terms of Service.
https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement 9/9