public static final int MONDAY = 0;
public static final int TUESDAY = 1;
public static final int WEDNESDAY = 2;
Chceme reprezentovat dny v týdnu.
public static final int MONDAY = 0;
public static final int TUESDAY = 1;
public static final int WEDNESDAY = 2;
Problémem je, že nemáme žádnou kontrolu:
2000
, a to nebude fungovat.public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
switch
public String tellItLikeItIs(Day day) {
switch (day) {
case MONDAY:
return "Mondays are bad.";
case FRIDAY:
return "Fridays are better.";
case SATURDAY:
case SUNDAY:
return "Weekends are best.";
default:
return "Midweek days are so-so.";
}
}
break
může být vynecháno, protože return
způsobí okamžitý návrat z funkce./