Skip to content
Snippets Groups Projects
Commit 6c9d335f authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Added more switch examples

Shows switching on integers, strings, and enums.
Shows all cases with break, shows intentional fall-through, and shows
idiomatic uncommented fall-through for two identical cases.
parent e745c96c
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ public class IfSwitch { ...@@ -18,6 +18,7 @@ public class IfSwitch {
System.out.println("You entered " + outputString + "."); System.out.println("You entered " + outputString + ".");
} }
// This version of convertToWord uses a chained conditional
private static String convertToWord(int value) { private static String convertToWord(int value) {
String numberText; String numberText;
if (value == 1) { if (value == 1) {
...@@ -33,6 +34,7 @@ public class IfSwitch { ...@@ -33,6 +34,7 @@ public class IfSwitch {
} }
/* /*
// This version of convertToWord uses a switch statement
private static String convertToWord(int value) { private static String convertToWord(int value) {
String numberText; String numberText;
switch (value) { switch (value) {
......
package edu.unl.cse.soft160.switches;
public class SwitchExamples {
public enum Temperature {
SUMMER, AUTUMN, WINTER, SPRING
}
// This example shows that switch statements can be used with integers
// This example also shows the typical situation in which each case ends with break
public static String digitToWord(int value) {
String numberWord;
switch (value) {
case 1:
numberWord = "one";
break;
case 2:
numberWord = "two";
break;
case 3:
numberWord = "three";
break;
case 4:
numberWord = "four";
break;
case 5:
numberWord = "five";
break;
case 6:
numberWord = "six";
break;
case 7:
numberWord = "seven";
break;
case 8:
numberWord = "eight";
break;
case 9:
numberWord = "nine";
break;
case 10:
numberWord = "ten";
break;
default: /* The default code should NOT be used for a normal case */
numberWord = "an out-of-range value";
}
return numberWord;
}
// This example shows that switch statements can be used with strings (in Java)
// This example also shows that you should add a "fall-through" comment when you intentionally omit a break
public static void findVehicleType(String vehicle) {
switch (vehicle) {
case "Silverado":
System.out.println("pickup truck");
break;
case "Traverse":
System.out.println("SUV");
break;
case "Corvette": /* The "Corvette" case will output "fast car" */
System.out.print("fast ");
// fall-through
case "Cruze":
System.out.println("car");
break;
default:
System.out.println("unrecognized vehicle");
}
}
// This example shows that switch statements can be used with enumerated types
// This example also shows the idiom where it's okay not to add a "fall-through" comment
public static String getSeasonalTemperature(Temperature season) {
String seasonalTemperature;
switch (season) {
case SUMMER:
seasonalTemperature = "hot";
break;
case WINTER:
seasonalTemperature = "cold";
break;
case SPRING: /* When two cases are identical, this is the idiom */
case AUTUMN:
seasonalTemperature = "warm";
break;
default:
seasonalTemperature = "indeterminate";
}
return seasonalTemperature;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment