-3

在下面的程序运行时,我无法到达 main 函数的末尾……我是 Java 新手,找不到它的缺陷。我需要你的帮助。谢谢。

import java.util.*;

class Schedule {

    public String day;
    private int startTime, endTime;

    public Schedule(String input_day, int input_start, int input_end) {
        day = input_day;
        startTime = input_start;
        endTime = input_end;
    }

    /* clashWith: to check whether this schedule clash with a Schedule called otherSchedule
     *      PRE-Condition  : input must be of Schedule type
     *      POST-Condition : return true if two Schedule clash, return false if not.
     */
    public boolean clashWith(Schedule otherSchedule) { 
        if(this.day != otherSchedule.day || this.endTime <= otherSchedule.startTime || this.startTime >= otherSchedule.endTime)
            return false;
        return true;
    }
}

class Module {

    String code;
    Schedule lecture, tutorial, lab;

    public Module(String input_code, Schedule input_lecture, Schedule input_tutorial, Schedule input_lab) {
        code = input_code;
        lecture = input_lecture;
        tutorial = input_tutorial;
        lab = input_lab;
    }

    /* count: to count number of classes(lecture, tutorial, and lab of only this Module) on day.
     *        For example: when day = "Monday", lecture is on Monday, tutorial is on Monday
     *        but lab is on Tuesday, then return 2. (lecture and tutorial are on Monday).
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public int count(String day) {
        int num = 0;
        if(lecture.day == day)
            num++;
        if(tutorial.day == day)
            num++;
        if(lab.day == day)
            num++;
        return num;
    }

    /* clashWith: to check whether this module clash with a Module called otherModule
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public boolean clashWith(Module otherModule) {
        if(lecture.clashWith(otherModule.lecture) || lecture.clashWith(otherModule.tutorial) || lecture.clashWith(otherModule.lab) )
            return true;
        if(tutorial.clashWith(otherModule.lecture) || tutorial.clashWith(otherModule.tutorial) || tutorial.clashWith(otherModule.lab))
            return true;
        if(lab.clashWith(otherModule.lecture) || lab.clashWith(otherModule.tutorial) || lab.clashWith(otherModule.lab))
            return true;
        return false;
    }
}

class Timetable {

    Vector<Module> listOfModule;

    /* checkClash: to check whether otherModule clash with one of 
     *             the modules in our timetable list.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public boolean checkClash(Module otherModule) {
        for(Module c: listOfModule)
            if(c.clashWith(otherModule))
                return true;
        return false;
    }

    /* add: to add a new module to the timetable list.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public void add(Module module) {
        listOfModule.add(module);
    }

    /* count: to count number of classes on day.
     *      PRE-Condition  :
     *      POST-Condition :
     */
    public int count(String day) {
        int count_day=0;
        for(Module c: listOfModule)
            count_day += c.count(day);
        return count_day;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num_operation;
        String code ;
        Timetable userTimetable = new Timetable();

        num_operation = input.nextInt();
        for(int i=0;i<num_operation;i++) {
            if(input.next() == "MODULE") {
                code = input.next();

                String day;
                int start, end;

                Schedule getLecSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
                Schedule getTutSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
                Schedule getLabSche = new Schedule(input.next(),input.nextInt(),input.nextInt());

                Module userModule = new Module(code, getLecSche, getTutSche, getLabSche);
                System.out.println("Reached line 162");
                if(!userTimetable.checkClash(userModule)) {
                    userTimetable.add(userModule);
                    System.out.println("Added");
                }
                else
                    System.out.println("Clashed");
            }
            else if(input.next() == "COUNT") {
                code = input.next();
                System.out.println(userTimetable.count(code));
            }
        }
    }
}
4

3 回答 3

2

找到了。你==用来比较Strings那只比较对象引用。

改用这个:

if (input.next().equals("MODULE"))

//...

if(input.next().equals("COUNT"))

还值得一提的是,这不会捕获“Count”、“cOunT”、“Module”、“module”或“mOdulE”——您可能想要使用它equalsIgnoreCase()

于 2012-09-09T15:45:17.920 回答
1

Timetable中,您永远不会为 赋值listOfModule

Vector<Module> listOfModule;

这会导致NullPointerException. 您需要为该引用分配一个新对象:

Vector<Module> listOfModule = new Vector<Module>();
于 2012-09-09T15:54:02.330 回答
-1

我认为问题出input.next() 在此处

公共字符串下一个()

从此扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是匹配分隔符模式的输入。即使先前调用 hasNext() 返回 true,此方法也可能在等待输入扫描时阻塞。

更新:
消除上述downvoters input.next()的混乱来自声明if(input.next() == "MODULE") {

于 2012-09-09T15:54:10.977 回答