Java Reference
In-Depth Information
if(yesNo=='n' || yesNo=='N') {
System.out.println("No selected");
} else {
if(yesNo=='y' || yesNo=='Y') {
System.out.println("Yes selected");
}
}
I prefer the switch statement as I think it's easier to follow, but you decide for yourself. Let's try an ex-
ample.
TRY IT OUT: Making the switch
This example uses a switch controlled by an integer type and a switch controlled by a variable of an
enumeration type:
public class TrySwitch {
enum WashChoice {cotton, linen, wool, synthetic}
// Define
enumeration type
public static void main(String[] args) {
// Variable to define the choice of wash
WashChoice wash = WashChoice.cotton;
// The clothes variable specifies the clothes to be washed by an
integer
// 1:shirts 2:sweaters 3:socks 4:sheets 5:pants
int clothes = 3;
switch(clothes) {
case 1:
System.out.println("Washing shirts.");
wash = WashChoice.cotton;
break;
case 2:
System.out.println("Washing sweaters.");
wash = WashChoice.wool;
break;
case 3:
System.out.println("Washing socks.");
wash = WashChoice.wool;
break;
case 4:
System.out.println("Washing sheets.");
wash = WashChoice.linen;
break;
case 5:
System.out.println("Washing pants.");
Search WWH ::




Custom Search