Switching on enums


/ Published in: Java
Save to your folder(s)

- Tiger simply requires that you not preface each enumerated type with the enum class name
- if not handling every enumerated type - warining will be displayed that not all of enum values not being compile time constants


Copy this code and paste it in your HTML
  1. public void testSwitchStatement(PrintStream out) throws IOException {
  2.  
  3. StringBuffer outputText = new StringBuffer(student1.getFullName( ));
  4.  
  5.  
  6.  
  7. switch (student1.getGrade( )) {
  8.  
  9. case A:
  10.  
  11. outputText.append(" excelled with a grade of A");
  12.  
  13. break;
  14.  
  15. case B: // fall through to C
  16.  
  17. case C:
  18.  
  19. outputText.append(" passed with a grade of ")
  20.  
  21. .append(student1.getGrade( ).toString( ));
  22.  
  23. break;
  24.  
  25. case D: // fall through to F
  26.  
  27. case F:
  28.  
  29. outputText.append(" failed with a grade of ")
  30.  
  31. .append(student1.getGrade( ).toString( ));
  32.  
  33. break;
  34.  
  35. case INCOMPLETE:
  36.  
  37. outputText.append(" did not complete the class.");
  38.  
  39. break;
  40.  
  41. }
  42.  
  43.  
  44.  
  45. out.println(outputText.toString( ));
  46.  
  47. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.