试图为我的代码获得正确的价值答案

时间:2013-10-03 21:00:24

标签: java netbeans

以下是我为学生班和主要方法编写的代码。我有两个问题。首先,当我尝试将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)

1 个答案:

答案 0 :(得分:2)

此行上的作业不正确:

score[i] = adder;

您已将adder初始化为0,因此您实际上在整个阵列中放置了零。难怪你得到0.0的平均值。而不是

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

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

total = total + score[i];