显示数组错误值

时间:2017-12-26 15:37:20

标签: java arrays string average

我正在做我的项目,我不知道我的代码有什么问题。它会要求您输入#ofSubjects,主题名称,学生姓名,所选科目的成绩,每位学生的平均成绩以及 最高平均值 < / strong>即可。每个学生的平均成绩是正确的,但是当我列出最高的平均成绩时,该值是不正确的。对不起我的英语,这不是我的第一语言,对不起我的问题,我还在学习。谢谢你,你会注意到这一点。这是代码

for (c=0; c<a; c++){
        System.out.print(e+ ". Name: ");
        students [c] = read.readLine();
        e++;
        for (f=0; f<b; f++){
            System.out.print("\t Grade in " +subjects[f] + ": ");
            grade [f] = Integer.parseInt(read.readLine());
           for (g = 0; g<b; g++ ){
                total [g] = grade[f] + grade[g];
                average [g] = total[g] / b;}          

        }
        System.out.println("Average: "+ (double)average[c] ); 
    }

    for (c=0; c<b; c++){
        System.out.print((double)average[c]+ "\n");}


/*Here's the output:

    MIDTERM EXAM: STUDENT RANKING
    Enter # of Student: 2
    Enter # of Subject: 2
    ----------------------------------
    Enter your 2 subjects:
    1. Java
    2. Phyton
---------------------------------
    1. Name: Proud
         Grade in Java: 90
         Grade in Phyton: 100
Average: 95.0
2. Name: Sone
     Grade in Java: 100
     Grade in Phyton: 65
Average: 65.0
82.0
65.0

The 82.0 should be 95.0.
*/

[编辑] 最后在评论中回答了这个问题!谢谢。但是出现了另一个问题,我正在尝试列出我输入的所有等级,但它似乎每次循环都会重置。因此,如果我从2名学生中输入两个2个科目,它将只记录我输入的最后2个科目。

示例:

MIDTERM EXAM: STUDENT RANKING
Enter # of Student: 2
Enter # of Subject: 2
----------------------------------
Enter your 2 subjects:
1. Java
2. Phyton
---------------------------------
1. Name: Proud
     Grade in Java: 90
     Grade in Phyton: 100
Average: 95.0
2. Name: Sone
     Grade in Java: 100
     Grade in Phyton: 65
Average: 82.5
95.0
82.5
List of Grades:
90
100
100
65

在我的情况下,它只记录第3和第4个数字,即100&amp;请帮助。

List of Grades:
100
65

1 个答案:

答案 0 :(得分:1)

您的平均数组位于错误的位置,应该在循环之外

尝试

    public void studentRanking() {
        Scanner read = new Scanner(System.in);

        System.out.println("MIDTERM EXAM: STUDENT RANKING");
        System.out.print("Enter # of Student: ");
        int noOfStudent = read.nextInt();
        System.out.print("Enter # of Subject: ");
        int noOfSubject = read.nextInt();

        String[] subjects = new String[noOfSubject];
        System.out.println("Enter your " + noOfSubject + " subjects: ");
        for (int i = 0 ; i < noOfSubject; i++) {
            System.out.print((i+1) + ". ");
            subjects[i] = read.next();
        }
        String[] students = new String[noOfStudent];
        double[] grade = new double[noOfSubject];
        double[] total = new double[noOfStudent];
        double[] average = new double[noOfStudent];
        double[] allGrades = new double[noOfStudent * noOfSubject];
        int t = 0;

        for (int i = 0, k = 0; i < noOfStudent; i++, k++) {
            System.out.print((i + 1) + ". Name: ");
            students[i] = read.next();
            for (int j = 0; j < noOfSubject; j++) {
                System.out.print("\t Grade in " + subjects[j] + ": ");
                grade[j] = read.nextDouble();
                allGrades[t++] = grade[j];
            }
            for (int g = 0; g < noOfSubject; g++) {
                total[k] += grade[g];
            }
            average[k] = total[k] / noOfSubject;
            System.out.println("Average: " + average[i]);
        }

        for (int c = 0; c < noOfStudent; c++) {
            System.out.println(average[c]);
        }
    }

<强>输出

MIDTERM EXAM: STUDENT RANKING
Enter # of Student: 2
Enter # of Subject: 2
Enter your 2 subjects: 
1. Java
2. Python
1. Name: Proud
     Grade in Java: 90
     Grade in Python: 100
Average: 95.0
2. Name: Sone
     Grade in Java: 100
     Grade in Python: 65
Average: 82.5
95.0
82.5