有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法为类实现tester程序

这是一个家庭作业,也是我的第一个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");



    }


}

}

共 (4) 个答案

  1. # 1 楼答案

    您已将StudentAverageTester创建为StudentAverage的非静态内部类。非静态内部类不允许有static声明,这就是您看到编译错误的原因(请参见JLS 8.1.3

    如果将StudentAverageTester类提取到它自己的StudentAverageTester.java文件中,效果会更好

  2. # 2 楼答案

    谢谢大家的意见和建议。这真的很有帮助。我按照建议创建了两个文件。一个用于我的测试人员,一个用于主类

    这是我的测试仪类代码

    class StudentTester {
    
        public static void main (String[] args){
    
            StudentAverage testAverage = new StudentAverage("Jose Mazorra", 50);
    
                testAverage.addQuiz(900);
                String name = testAverage.getName();
    
                double avg = testAverage.getAverageScore();
    
                System.out.println(name);
                System.out.println(avg);
    
    
        }
    
    }
    

    创建单独的文件比我最初做的要容易得多,我最初做的是将所有内容都放在一个文件中。更干净,更容易维护。 我还对我的主clase代码进行了一些修改:

        /**
       A student who is taking quizzes.
    */
    public class StudentAverage
    { 
        //Instances variables
        private String name;
        private double quizScores;
        private double numOfQuizzesTaken;
    
        /**
         * Constructs a Student object with a name "Jose Mazorra" and zero total quiz score
         */
        public StudentAverage()
        {
            name = "Jose mazorra";
            quizScores = 0;
            numOfQuizzesTaken = 1;
        }
    
        /**
         * Constructs a Student object with an initial name and total quiz score
         */
        public StudentAverage(String pName, double pQuizScore)
        {
            name = pName;
            quizScores = pQuizScore;
            numOfQuizzesTaken = 20;
        }
    
    
       /**
          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)
       {  
           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;
    
    }
    
    }
    
  3. # 3 楼答案

    您正在创建一个类和另一个类。我认为你不打算那样做。此外,如果这两个类都在一个文件中,则只有一个类可以是公共的。最好将每个类移动到其单独的文件中

    更新:

    So the Issue here is that you are creating Inner class and in Java inner classes cannot have static methods.Because an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.

  4. # 4 楼答案

    您的代码中有一些错误。首先,照大家说的做,把StudentAverageTester放在自己的文件中。当前,您正在StudentAverage类中声明。您也没有在StudentAverage类中声明无参数构造函数。在StudentAverageTester中,您有

    StudentAverage student = new StudentAverage()
    

    但应该是这样

    StudentAverage student = new StudentAverage("Some name")
    

    你还忘了分号

    更新

    您的name属性是私有的。在StudentAverageTester中不能这样访问它。您需要声明一个setter方法,如setName(String name)所示

    <>考虑查看你的^ {CD1>}类。您正在调用未定义的方法并直接访问私有成员。你不能那样做。使用setter和getter