使用switch语句计算字母等级。
/* nowjava.com - 时 代 Java */ import java.util.Scanner; public class Main { public static void main(String[] args) { int total = 0; // sum of grades int gradeCounter = 0; // number of grades entered int aCount = 0; // count of A grades int bCount = 0; // count of B grades int cCount = 0; // count of C grades int dCount = 0; // count of D grades int fCount = 0; // count of F grades Scanner input = new Scanner(System.in);//来 自 N o w J a v a . c o m - 时 代 Java System.out.printf("%s%n%s%n %s%n %s%n", "Enter the integer grades in the range 0-100.", "Type the end-of-file indicator to terminate input:", "On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter", "On Windows type <Ctrl> z then press Enter"); // loop until user enters the end-of-file indicator while (input.hasNext()) { int grade = input.nextInt(); // read grade total += grade; // add grade to total ++gradeCounter; // increment number of grades // increment appropriate letter-grade counter switch (grade / 10) { case 9: // grade was between 90 case 10: // and 100, inclusive ++aCount; break; // exits switch case 8: // grade was between 80 and 89 ++bCount; break; // exits switch case 7: // grade was between 70 and 79 ++cCount; break; // exits switch case 6: // grade was between 60 and 69 ++dCount; break; // exits switch default: // grade was less than 60 ++fCount; break; // optional; exits switch anyway } // end switch } // end while // display grade report System.out.printf("%nGrade Report:%n"); // if user entered at least one grade... if (gradeCounter != 0) { // calculate average of all grades entered double average = (double) total / gradeCounter; // output summary of results System.out.printf("Total of the %d grades entered is %d%n", gradeCounter, total); System.out.printf("Class average is %.2f%n", average); System.ou