0

以下は、学生クラスとメインメソッド用に作成したコードです。私は2つの問題を抱えています。まず、メインを独自のクラスとして配置しようとすると、実行とコンパイルに失敗した場合、メインがメイン内の学生クラスを参照および作成できないという誤ったエラーが発生します。

2番目の問題は、最高の平均マークを出力する最後の行で、常に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);

}
}

編集: これは別のクラスのコードであり、メインの実行時に発生するエラー メッセージです。

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