Java Reference
In-Depth Information
wash = WashChoice.synthetic;
break;
default:
System.out.println("Unknown washing - default synthetic.");
wash = WashChoice.synthetic;
break;
}
System.out.println("Wash is "+ wash);
// Now select the wash temperature
switch(wash) {
case wool:
System.out.println("Temperature is 120.");
break;
case cotton:
System.out.println("Temperature is 170.");
break;
case synthetic:
System.out.println("Temperature is 130.");
break;
case linen:
System.out.println("Temperature is 180.");
break;
}
}
}
TrySwitch.java
You should get the following output from this example:
Washing socks.
Wash is wool
Temperature is 120.
How It Works
This looks like a lot of code, but it's because of the number of cases in the two switch statements. Ob-
viously you don't really need to use two switch statements here, but I used two to show integers and
enumeration constants as case values.
You first define an enumeration type, WashChoice . You then define a variable of this type in the main()
method with the following statement:
WashChoice wash = WashChoice.cotton; // Variable to define the
choice of wash
The initial value for wash here is arbitrary. You could have chosen any of the possible enumeration con-
stants for the WashChoice type.
Next, you define and initialize a variable identifying the type of clothes to be washed:
int clothes = 3;
Search WWH ::




Custom Search