0

下面是我为学生类和主要方法编写的代码。我有两个问题。首先,当我尝试将 main 作为自己的类时,如果无法运行和编译,则表示 main 存在错误错误,无法在 main 中引用和创建学生类。

第二个问题,它打印出最高平均分的最后一行,总是打印出 0.0,我终生无法弄清楚为什么。

谁能给我解决这两个问题的方法?

我正在使用 NetBeans。

package student;

public class Student {

    private String name, id;
    private int[] score = new int[3];
    public Student()
        {

        }
        public Student(String stName, String stID, int stScore[]) {
        this.name = stName;
        this.id = stID;
        this.score = stScore;    
    }
    public void setName(String nameIn)
    {
        name = nameIn;
    }
    public String getName()
    {
        return name;
    }
    public double avScore()
    {
        double total = 0;
        int to = 0;
        int adder = 0;
        for (int i=0; i<score.length; i++)
        {
            score[i] = adder;
            total = total + adder;
        }
        total = total / score.length;
        return total;
    }
    public void printOut() {
        System.out.println("Student Name is: " + name) ;
        System.out.println("Student ID is: " + id);
        System.out.println("Student scores are: ");
        for (int i=0; i<score.length; i++)
        {
            System.out.println(score[i]);
        }
    }

    public static void main(String args []) {
        Student stud1 = new Student("Nico Del Pellegrino", "up660537", new int[] {1, 2, 3});
        Student stud2 = new Student("Dylan Scott", "up652312", new int[] {5, 7, 13});
        stud1.printOut();
        stud2.printOut();
        Student stud3 = new Student();
        stud3.id = "up645658";
        stud3.name = "Alex Barrett";
        stud3.score = new int[]{5, 10, 15};
        stud3.printOut();
        double stud1Score = stud1.avScore();
        double stud2Score = stud2.avScore();
        double stud3Score = stud3.avScore();
        double[] scoreList = {stud1Score, stud2Score, stud3Score};
        double highestMark = 0;
        for (int i=0; i<scoreList.length;)
        {
            if(scoreList[i]>highestMark)
            {
                highestMark = scoreList[i];
                i++;
            }
            else
            {
                i++;
            }
        }
        System.out.println("The highest average mark is: " + highestMark);

}
}

编辑:这是其单独类中的代码,以及运行 main 时出现的错误消息。

package student;

public class Student {

private String name, id;
private int[] score = new int[3];

public Student() {
}

public Student(String stName, String stID, int stScore[]) {
    this.name = stName;
    this.id = stID;
    this.score = stScore;
}

public void setName(String nameIn) {
    name = nameIn;
}

public String getName() {
    return name;
}

public double avScore() {
    double total = 0;
    int to = 0;
    for (int i = 0; i < score.length; i++) {
        total = total + score[i];
    }
    total = total / score.length;
    return total;
}

public void printOut() {
    System.out.println("Student Name is: " + name);
    System.out.println("Student ID is: " + id);
    System.out.println("Student scores are: ");
    for (int i = 0; i < score.length; i++) {
        System.out.println(score[i]);
    }
}
}


package Student;

import Student.*;

public class Main {

public static void main(String args []) {
    //Create two student objects stud1 and stud2 here
    Student stud1 = new Student("Nico Del Pellegrino", "up660537", new int[] {1, 2, 3});
    Student stud2 = new Student("Dylan Scott", "up652312", new int[] {5, 7, 13});
    //Display information for the two objects
    stud1.printOut();
    stud2.printOut();
    //Create third student object stud3 here
    Student stud3 = new Student();
    // change object id
    stud3.id = "up645658";
    // change object name
    stud3.name = "Alex Barrett";
    // change object exam scores
    stud3.score = new int[]{5, 10, 15};
    stud3.printOut();
    // Find out which student is with the highest average score
    int stud1Score = stud1.avScore();
    int stud2Score = stud2.avScore();
    int stud3Score = stud3.avScore();
    //Display his/her details here
    }
    }


    //run:
    //Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous         tree //type: Student.Student
    //  at Student.Main.main(Main.java:9)
    //Java Result: 1
    //BUILD SUCCESSFUL (total time: 0 seconds)
4

1 回答 1

2

您在此行的分配不正确:

score[i] = adder;

您初始化adder0,因此您在整个数组中有效地放置了零。难怪你得到0.0了平均水平。代替

score[i] = adder;
total = total + adder;

你甚至不需要adder,只需使用:

total = total + score[i];
于 2013-10-03T21:05:38.677 回答