這是一項家庭作業,也是我的第一個Java程序。我寫了一個StudentAverage類,現在我想測試類方法,但是當我編寫我的測試程序時,IDE告訴我不能聲明我的主要靜態方法。我使用Eclipse作爲我的IDE。無法實現類的測試程序
由於這是一項家庭作業,我仍然在學習Java,所以我也希望得到一些關於我在做什麼錯誤的指導。
這裏是我的代碼:
/**
*Program: StudentAverage, Calculate the student average quizzes taken
* @author: Jose Mazorra
* Date: July 11, 2013
* Class: CIS406
*/
/**
A student who is taking quizzes.
*/
public class StudentAverage
{
//Instances variables
private String name;
private double quizScores;
private double numOfQuizzesTaken;
/**
Constructs a student with a given name.
@param n the name
*/
public StudentAverage(String stuName)
{
name = (stuName);
}
/**
Gets the name of this student.
@return the name
*/
public String getName()
{
return name;
}
/**
Adds a quiz score.
@param score the score to add
*/
public void addQuiz(int score)
{
numOfQuizzesTaken++;
quizScores = quizScores + score;
}
/**
Gets the sum of all quiz scores.
@return the total score
*/
public double getTotalScore()
{
return quizScores;
}
/**Returns the average of all quiz taken
* by the student
* @return average score
*/
public double getAverageScore(){
double avgScore;
avgScore = (quizScores/numOfQuizzesTaken);
return avgScore;
}
public class StudentAverageTester{
public static void main(String[] args){
StudentAverage student = new StudentAverage()
student.name("Jose");
student.numOfQuizzesTaken(10);
student.quizScores(400);
double avg = student.avgScore();
System.out.println(name);
System.out.println(avg);
System.out.println("Expected 40");
}
}
}
這兩個公共類都在同一個文件中嗎? –
刪除第二個類中的public關鍵字或將其移到它自己的單獨文件 – Andy
考慮閱讀jUnit並使用它來測試您的代碼。 –