Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法修复Java中的/by-zero错误_Java - Fatal编程技术网

无法修复Java中的/by-zero错误

无法修复Java中的/by-zero错误,java,Java,我试着用我所知道的方法来解决这个问题,但我现在还不知道,我的教授是一个完全的工具,如果你请求帮助,他会贬低你,请帮助。这是我通过这门课必须做的项目之一 public class Student { private String name; //name of the student private String id; //student id private String major; //student’s

我试着用我所知道的方法来解决这个问题,但我现在还不知道,我的教授是一个完全的工具,如果你请求帮助,他会贬低你,请帮助。这是我通过这门课必须做的项目之一

public class Student {
    private String name;           //name of the student
    private String id;             //student id
    private String major;          //student’s major: CSCI,MATH,NURS, all others are XXXX
    private int    completedHours; //number of hours the student has completed
    private int    qualityPoints;  //number of overall quality points a student has completed
    private char   studentType;    //type of student G (graduate) U (undergraduate) X (invalid type)

    public Student() {

    }

    public Student(String name) {
        setName(name);
    }//end Student()

    public Student(String name, String id, String major, char studentType) {
        setName(name);
        setId(id);
        setMajor(major);
        setStudentType(studentType);
    }//end Student(String,String,String,char)

    public void setName(String Name) {
        this.name = name;
    } //end setName(String)

    public void setId(String id) {
        this.id = id;
    }//end setId(String)

    public void setMajor(String major) {
        this.major = major;
    }//end setMajor(String)

    public void setCompletedHours(int hours) {
    hours = completedHours;
    }//end setCompletedHours(int)

    public void setQualityPoints(int points) {
        points = qualityPoints;
    }//end setQualityPoints(int)

    public void setStudentType(char type) {
        if (type == 'u' && type == 'U')
            type = 'U';
        else if (type == 'g' && type == 'G')
            type = 'G';
        else 
            type = 'X';

        type = studentType;
    }//end setStudentType(char)

    public String getName() {
        return name;
    }//end getStudentName()

    public String getId() {
        return id;
    }//end getId()

    public String getMajor() {
        return major;
    }//end getMajor()

    public int getCompletedHours() {
        return completedHours;
    }//end getCompletedHours()

    public int getQualityPoints() {
        return qualityPoints;
    }//end getQualityPoints()

    public char getStudentType() {
        return studentType;
    }//end 

    public double gpa() {
        double gpa;

        gpa = qualityPoints / completedHours;

        return gpa;
    }

    public void addCompletedHours(int hours) {
        if ( hours<0) {
            completedHours = completedHours;
        } else {
            if (hours >=0)
                completedHours += hours;
        }
    }

    public String classification() {
        String strClassification;
        strClassification = "A";

        if (studentType == 'G')
            strClassification = "Graduate";
        else if (studentType == 'X')
            strClassification = "Invalid Student Type";
        else if (completedHours<30)
            strClassification = "Freshman";
        else if (completedHours<60)
            strClassification = "Sophomore";
        else if (completedHours<90)
            strClassification = "Junior";
        else if (completedHours>90)
            strClassification = "Senior";

        return strClassification;
    }

    public String studentRecord() {
        String  strStudentRecord;
        strStudentRecord = ("\n Name:\t\t" + name + "\n ID:\t\t\t"  + id + "\nMajor:\t\t" + major + "\nCompletedHrs: " + completedHours + "\nQuality Pts:\t" + qualityPoints + "\nGPA:\t\t\t" + gpa() + "\nClassification:   " + classification());

        return strStudentRecord;
    }

}
类别3:

public class Project3 {

    public static void main(String[] args) {
        String strStudent1;

        Student stu1 = new Student("John Smith", "1234B", "Nursing", 'U');
        stu1.setCompletedHours(34);
        stu1.setQualityPoints(85);

        strStudent1 = stu1.studentRecord();

        System.out.print(strStudent1);
    }//end main
}//end Project3

你的二传手错了

这:

应该是这样的:

public void setCompletedHours(int hours)
{
    completedHours = hours;
}//end setCompletedHours(int)

public void setQualityPoints(int points)
{
    qualityPoints = points;

}//end setQualityPoints(int)
你没有在二传中设置任何内容。因此,
qualityPoints
completedHours
都是零

然后,当您调用
gpa()
时,它试图除以零。
顺便说一句,下次试着调试它,你会看到你的值。

如何定义
completedHours
?你能提供整个
stu1
类吗?粘贴足够的代码以便我们了解流程。你能提供更多的代码吗?什么是“stu1”?第一步:停止除以零。说真的:我们不知道那些二传手到底在干什么。提示二:
gpa
是一行程序。我编辑并添加了我所有的代码,谢谢你的帮助。非常感谢,非常感谢。我对这一切都是全新的,所以这是一个很大的帮助
public void setCompletedHours(int hours)
{
    hours = completedHours;
}//end setCompletedHours(int)

public void setQualityPoints(int points)
{
    points = qualityPoints;

}//end setQualityPoints(int)
public void setCompletedHours(int hours)
{
    completedHours = hours;
}//end setCompletedHours(int)

public void setQualityPoints(int points)
{
    qualityPoints = points;

}//end setQualityPoints(int)