创建一个班级来记录学生成绩

时间:2015-04-01 16:25:26

标签: java class methods

我知道我的代码中有很多错误,但我找不到它们/不理解它们。我究竟做错了什么?

更新:感谢您的帮助。我已设法清除除一个之外的错误。我仍然在getGradeScore上遇到错误。

错误:找不到符号

错误:非法启动类型


public class StudentRecord
{
    /***DECLARATIONS**/

    private int quizScore1;
    private int quizScore2;
    private int quizScore3;            
    private int midtermScore;
    private int finalScore;

    /***ACCESSOR METHODS**/

    public int getQuizScore1()
    {
        return quizScore1;
    }

    public int getQuizScore2()
    {
        return quizScore2;
    }

    public int getQuizScore3()
    {
        return quizScore3;
    }

    public int getMidtermScore()
    {
        return midtermScore;
    }

    public int getFinalScore()
    {
        return finalScore;
    }

    /***MUTATOR METHODS**/

    public void setQuizScore(int quizScore1, int quizScore2, int quizScore3)
    {
        if (quizScore1 < 0 || quizScore1 > 10)
        {
            System.out.println("Error: Invalid Quiz Grade"); 
        }
        if (quizScore2 < 0 || quizScore2 > 10)
        {
            System.out.println("Error: Invalid Quiz Grade");
        }
        if (quizScore3 <0 || quizScore3 > 10)
        {
            System.out.println("Error: Invalid Quiz Grade");
        }
        else
        {
            this.quizScore1 = quizScore1;
            this.quizScore2 = quizScore2;
            this.quizScore3 = quizScore3;
        }
    }

    public void setMidtermScore(int midtermScore)
    {
        if (midtermScore < 0 || midtermScore > 100)
        {
            System.out.println("Error: Invalid Midterm Score");
        }
        else
        {
            this.midtermScore = midtermScore;
        }
    }

    public void setFinalScore(int finalScore)
    {
        if (finalScore < 0 || finalScore > 100)
        {
            System.out.println("Error: Invalid Final Score");
        }
        else
        {
            this.finalScore = finalScore;
        }
    }

    /***OTHER METHODS***/

    public void studentRecord()
    {
        quizScore1 = 0;
        quizScore2 = 0;
        quizScore3 = 0;
        midtermScore = 0;
        finalScore = 0;
    }

    public void studentRecord(int quizScore1, int quizScore2, int quizScore3, int midtermScore, int finalScore)
    {
        System.out.println("Quiz Score 1: " + quizScore1);
        System.out.println("Quiz Score 2: " + quizScore2);
        System.out.println("Quiz Score 3: " + quizScore3);
        System.out.println("Midterm Score: " + midtermScore);
        System.out.println("Final Score: " + finalScore);
    }

    /***EQUALS METHODS***/

    public boolean equalsQuizScore1 (int otherQuizScore1)
    {
        return (quizScore1 == otherQuizScore1);
    }

    public boolean equalsQuizScore2 (int otherQuizScore2)
    {
        return (quizScore2 == otherQuizScore2);
    }

    public boolean equalsQuizScore3 (int otherQuizScore3)
    {
        return (quizScore3 == otherQuizScore3) ;
    }

    public boolean equalsMidtermScore (int otherMidtermScore)
    {
        return (midtermScore == otherMidtermScore);
    }

    public boolean equalsFinalScore (int otherFinalScore)
    { 
        return (finalScore == otherFinalScore);
    }

    /***GET GRADE METHODS***/

    public int getGradeScore()
    {
        int gradeScore;

        gradeScore = (((quizScore1 + quizScore2 + quizScore3) / 3) / 10 * 25) + (midtermScore / 100 * 35) + (finalScore / 100 * 40); 

        return gradeScore;
    }

    public char getLetterGrade()
    {
        int gradeScore;
        char letterGrade;

        if (gradeScore >= 90 && gradeScore <= 100)
        {
            return 'A';
        }
        else if (gradeScore >= 80 && gradeScore <= 89)
        {
            return 'B';
        }
        else if (gradeScore >= 70 && gradeScore <= 79)
        {
            return 'C';
        }
        else if (gradeScore >= 60 && gradeScore <= 69)
        {
            return 'D';
        }
        else 
        {
            return 'F';
        }
    }

    public String toString()
    {
        int gradeScore;
        char letterGrade;

        return ("Grade Score is " + gradeScore + "and letter grade is " + letterGrade);
    }

}

任何帮助表示赞赏!我花了好几个小时就搞清楚了。我对课程和方法很困惑。

3 个答案:

答案 0 :(得分:1)

如果您尝试编译代码,则会出现大量语法错误。以下是我发现的3个主要违规者和一些提议的解决方案

1)quizScore1,quizScore2和quizScore3都是未声明的。但是,你有一个quizScore变量。如果要存储3个quizScores,您可以列出分数,也可以声明它们。我建议使用ArrayList。您还需要添加更多get函数,每个分数一个。

2)public boolean equals (otherQuizScore)是不允许的。 otherQuizScore的类型是什么?添加类型。

3)多个返回语句。 return语句将结束该函数。如果连续有3个返回语句,则第二个2将从不运行。

您的代码中存在大量错误。如果你将它放入IDE(我建议Eclipse,如果你还没有它),你会看到每个错误都标有红色下划线,并带有相应的错误信息。

答案 1 :(得分:0)

    public int getQuizScore()

在上面的方法中你要返回三个值。这是不可能的。只需将单独的三种方法用于返回quizScore1 , quizScore2,quizScore3

如果您需要有关代码的任何帮助,请通知我

答案 2 :(得分:0)

^同样在你的public int getGradeScore()方法中,你缺少一个int类型的return语句。如果方法不是无效的,它必须返回一些东西。在你的情况下:int。