-1

当我运行我的代码时出现此错误,不确定这里有什么问题:

Exception in thread "main" java.lang.VerifyError: (class: first3weeks/Main, method: <init> signature: ()V) Constructor must call super() or this()
Java Result: 1

学生代码:

package first3weeks;   

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


    public Student(){}

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

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

    public void setID(String idIn){
        id = idIn;
    }

    public void setScore(int scoreIn[]){
        score = scoreIn;
    }

    public String getName(){
        return name;
    }

    public String getID(){
        return id;
    }

    public int[] getScore(){
        return score;
    }

    public double avScore(){
        double total = score[1] + score[2] + score[3];
        return (total/3);
    }

    public void printOut(){
        System.out.println("Student Name: " + getName() + "\n" + "Student ID: " + getID() + "\n" + "Student Average: " + avScore());
    }
}

主类:

package first3weeks;

public class Main {

    public static void main(String[] args) {
        int[] score1 = {12,15,19};
        int[] score2 = {32,65,29};
        Student stud1 = new Student("Rob", "001", score1);
        Student stud2 = new Student("Jeff", "002", score2);
        stud1.printOut();
        stud2.printOut();

        Student stud3 = new Student();
        int[] score3 = {56,18,3};
        stud3.setName("Richard");
        stud3.setID("003");
        stud3.setScore(score3);
        stud3.printOut();
    }
}
4

2 回答 2

2

这个错误

Exception in thread "main" java.lang.VerifyError: (class: first3weeks/Main, 
    method: <init> signature: ()V) Constructor must call super() or this()

表示未正确生成字节码。这可能是编译器中的错误。我会确保您拥有 Java 7 update 40 或 Java 6 update 45 的最新更新。

于 2013-10-13T15:08:50.737 回答
1

我使用 java 版本运行您的代码1.7.0_17,我得到的唯一例外是java.lang.ArrayIndexOutOfBoundsException: 3.**

在java中,Array是从零开始的索引,即第一个元素的索引为零,所以在avScore你应该做的方法中:

 public double avScore(){
       double total = score[0] + score[1] + score[2];
       return (total/3);
 }
于 2013-10-13T15:05:37.893 回答