previous | start | next

Example 3 (cont.)

    1   public class GradeComp {
    2   
    3     public static String letterGrade(int score) {
    4       String grade;
    5       if ( score >= 94 ) {
    6         grade = "A";
    7       } else if ( score >= 85 ) {
    8         grade = "B";
    9       } else if ( score >= 76 ) {
   10         grade = "C";
   11       } else if ( score >= 67 ) {
   12         grade = "D";
   13       } else {
   14         grade = "F";
   15       }
   16       return grade;
   17     }
   18     
   19     public static void main(String[] args) {
   20       int score;
   21       String grade;
   22       Scanner input = new Scanner(System.in);
   23       
   24       System.out.print("Enter a test score: ");
   25       score = input.nextInt();
   26       
   27       grade = letterGrade(score);
   28       
   29       System.out.printf("For numeric score %d, the grade is %s", score, grade);
   30     }
   31   }


previous | start | next