成绩的最低,最高和平均

时间:2016-11-18 18:52:53

标签: java

我正在尝试编写一个程序,该程序接受成绩数字并返回字母成绩,在用户输入-1后,它返回最高成绩,最低成绩和输入的平均成绩。现在,该程序返回字母等级,并为平均值,最大值和最小值返回零。

import java.util.Scanner;

public class getLetterGrade {
    public static void main(String[] args) {
        int count = getGrades();
        int sum = getGrades();
        int max =  getGrades();
        int min =  getGrades();
        System.out.println("You entered " + count + " students");
        System.out.println("Class average =" + sum / count);
        System.out.println("Maximum =" + max);
        System.out.println("Minimum ="+ min);

    }

    public static int getGrades() {
        Scanner reader = new Scanner(System.in); {
        int count = 0;
        int sum = 0;
        int min = 100;
        int max = 0;

        while(true) {
            System.out.println("Welcome to the grade calculator. \n Please enter a numeric grade. \n After the last student in the class, enter a grade of -1.");


            int grade = reader.nextInt();



            if (grade >= 90) {
                System.out.println("That's an A");
            } else if (grade >= 80 ) {
                System.out.println("That's a B");
            } else if (grade >= 70) {
                System.out.println("That's a C");
            } else if (grade >= 60) {
                System.out.println("That's a D");
            } else if (grade >= 0) {
                System.out.println("That's an F");
            } else if (grade == -1) {
                return count; 
            }

            if (grade < min){
              min = grade;
            }
            if (grade > max){
              max = grade;}

            count++;
            sum += grade;

        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

在您的主要功能中,查看您是如何计算数,总和,最大值和最小值的。如果getGrades()仅在您获得-1时才返回计数,getGrades()如何为您提供任何值?

然后你又打电话给getGrades()四次。你需要要求用户输入4次吗?

仅通过调用getGrades()一次修复它,然后将打印语句移到getGrades()中的while循环之后。

同时更改getGrades()以便它不会返回任何内容,只需在用户输入-1时使其退出while循环。

就像其他人所说的那样,我恳请你和教授,ta或其他任何人亲自向你解释这个问题。

public static void getGrades() {
    Scanner reader = new Scanner(System.in); 
    int count = 0;
    int sum = 0;
    int min = 100;
    int max = 0;

    while(true) {
        System.out.println("Welcome to the grade calculator. \n Please enter a numeric grade. \n After the last student in the class, enter a grade of -1.");

        int grade = reader.nextInt();

        if (grade >= 90) {
            System.out.println("That's an A");
        } else if (grade >= 80 ) {
            System.out.println("That's a B");
        } else if (grade >= 70) {
            System.out.println("That's a C");
        } else if (grade >= 60) {
            System.out.println("That's a D");
        } else if (grade >= 0) {
            System.out.println("That's an F");
        } else if (grade == -1) {
            // Exit while loop to finish the rest of the code
            break; 
        }

        if (grade < min){
          min = grade;
        }
        if (grade > max){
          max = grade;
        }

        count++;
        sum += grade;
    }
    // Moved print statements here, to be just outside the while loop
    System.out.println("You entered " + count + " students");
    System.out.println("Class average =" + sum / count);
    System.out.println("Maximum =" + max);
    System.out.println("Minimum ="+ min);
}