java - 我在编写测试驱动程序时遇到了麻烦

标签 java testing compiler-errors

我第一次伸出援手来解决问题,不确定是什么原因引起的。我有我编写的这两个类,作业要求我做一个测试驱动程序来证明这些类有效。

学生类:

public class Student{
    private Course[] courseList;
    private static int numCourses;
    private final int maxCourses;

public Student(int max){
    maxCourses = max;
    courseList = new Course[numCourses];
    numCourses = 0;
}
// part 1, done
public boolean addCourse(Course newClass){
    boolean success = false;

for(int i=0; i<=maxCourses; i++){

    if(i == numCourses){
    courseList[i] = newClass;
    success = true;
    numCourses++;
    }
}
return success;
}
// part 2, done
public boolean dropCourse(Course oldClass){
    boolean success = false;

for(int i=0; i<=courseList.length; i++){
    if (courseList[i] == oldClass){
        courseList[i] = null;
        numCourses--;
        success = true;
    }
}
return success;
}
// part 3, done.
    public int getNumCourses(){
    return numCourses;
    }
//part 4, done
    public boolean isFullTime(){
        boolean success = false;
        if (numCourses >= 3){
            success = true;
        }
        return success;
    }
// part 5, done
    public String getClassList(){
        String list = "";

    for(int i=0;i<=numCourses; i++){
        list = courseList[i].getID() + "\t" + courseList[i].getName() + "\n";

    }
    return list;
    }
}

和类(class):
public class Course{
    private String name;
    private int id;
    private static int nextID = 200000;

    public Course(String nameIn)
    {
    name = nameIn;
    id = nextID;
    nextID++;
    }
public String getName(){
return name;
}
public int getID(){
return id;
}
}

由于某种原因,如果我将一个测试驱动程序简化为:
public class tester{

public static void main(String[] args){
    Course one = new Course(Java);
}
}

我的参数出现错误,提示找不到符号
javac tester.java
tester.java:6: error: cannot find symbol
        one = new Course(name);
                         ^
  symbol:   variable name
  location: class tester
1 error

我有一个更长的测试驱动程序,但是它没有超过前几行,因为这是相同的错误,只是相同错误中的几个。

谢谢你的时间。
编辑:我误将Student类放入了两次,在我的简短测试驱动程序中,唯一使用的类是Course类。

最佳答案

问题在于,由于Java周围没有引号,因此编译器将其用作变量名。由于未定义,因此编译器无法找到它。使用Course one = new Course("Java");

关于java - 我在编写测试驱动程序时遇到了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804533/

相关文章:

reactjs - 如何系统测试实时 react 多人游戏?

c# - 我是一个初学者,我无法解决C#代码中的一些错误

编译警告。数组初始值设定项中的元素过多

c# - 编译器错误1 : constructor contains more than 0 args

java - 使用 JUnit 测试 Tapestry 页面和组件

javascript - Mocha 如何知道在测试套件中首先加载哪个文件

java - ListView 内的 ListView 项目

windows - 使用digest.bat 创建摘要并退出且未配置JRE_HOME

java - Spring Boot带附件的邮件发送

java - 为什么我的 Web 应用程序可以在 Glassfish 上运行,但不能在 Tomcat 服务器上运行?