Java Reference
In-Depth Information
Grades()
{
range = "";
}
Grades(String str)
{
range = str;
}
Example D-4 illustrates how the
enum
type
Grades
works.
EXAMPLE D-4
public class
EnumExample2
{
public static void
main(String[] args)
{
System.out.println("Grade Ranges");
//Line 1
for
(Grades gr : Grades.values())
//Line 2
System.out.println(gr + " "
+ gr.getRange());
//Line 3
System.out.println();
//Line 4
}
}
Sample Run:
Grade Ranges
A Range 90% to 100%
B Range 80% to 89.99%
C Range 70% to 79.99%
D Range 60% to 69.99%
F Range 0% to 59.99%
The foreach loop in Line 2 uses the method
values
to retrieve the
enum
constants as a
list. The method
getRange
in Line 3 is used to retrieve the string contained in the
Grades
object.
The following programming example uses an
enum
type to create a program to play the
game of rock, paper, and scissors.
Search WWH ::
Custom Search