如何修复 Java 程序中错误的平均分数计算?

问题描述 投票:0回答:1

我正在开发一个Java程序,该程序以学生成绩作为输入,计算他们的成绩,并计算平均分数。但是,我遇到了平均分数计算不正确的问题。 这是我正在使用的代码:

import java.util.Scanner;

public class StudentGrades {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 0;
        double sum = 0.0;

        while (true) {
            System.out.print("Enter student score (-1 to exit): ");
            double score = scanner.nextDouble();

            if (score == -1) {
                break;
            }

            if (score < 0 || score > 100) {
                System.out.println("Invalid score. Please enter a score between 0 and 100.");
                continue;
            }

            count++;
            sum += score;

            char grade;
            if (score >= 90) {
                grade = 'A';
            } else if (score >= 80) {
                grade = 'B';
            } else if (score >= 70) {
                grade = 'C';
            } else if (score >= 60) {
                grade = 'D';
            } else {
                grade = 'F';
            }

            System.out.println("Grade: " + grade);
        }

        if (count > 0) {
            double average = sum / count;
            System.out.println("Total scores processed: " + count);
            System.out.printf("Average score: %.2f%n", average);
        } else {
            System.out.println("No scores were entered.");
        }

        scanner.close();
    }
}

在用各种分数测试程序时,我注意到显示的平均分数与我的计算不一致。例如,输入分数 85、75 和 95 后,我预计平均值为 85. 00,但结果不正确。

我检查了代码中的计算逻辑并确保我输入了有效的分数。我期望平均分数能够反映正确的计算,但事实并非如此。

java math
1个回答
0
投票

我试试这个

import java.util.Scanner;

public class StudentGrades {
  public static void main(String[] 
  args) 
{
    Scanner scanner = new Scanner(System.in);
    int count = 0;
    double sum = 0.0;

    while (true) {
        System.out.print("Enter student score (-1 to exit): ");
        double score = scanner.nextDouble();

        if (score == -1) {
            break;
        }

        if (score < 0 || score > 100) {
            System.out.println("Invalid score. Please enter a score between 0 and 100.");
            continue;
        }

        count++;
        sum += score;
        System.out.println("Current Sum: " + sum + ", Count: " + count); // Debugging line

        char grade;
        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else if (score >= 70) {
            grade = 'C';
        } else if (score >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }

        System.out.println("Grade: " + grade);
    }

    if (count > 0) {
        double average = sum / count;
        System.out.println("Total scores processed: " + count);
        System.out.printf("Average score: %.2f%n", average);
    } else {
        System.out.println("No scores were entered.");
    }

    scanner.close();
}
}
© www.soinside.com 2019 - 2024. All rights reserved.