在程序下运行时,我无法到达主要功能的结尾... 我是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));
}
}
}
}
答案 0 :(得分:2)
找到它。您正在使用==
来比较Strings
。 That only compares the object reference.
请改用:
if (input.next().equals("MODULE"))
//...
if(input.next().equals("COUNT"))
还值得一提的是,这不会捕获“Count”,“cOunT”,“Module”,“module”或“mOdulE” - 您可能希望使用equalsIgnoreCase()
代替。
答案 1 :(得分:1)
在Timetable
中,您永远不会为listOfModule
分配值:
Vector<Module> listOfModule;
导致NullPointerException
。您需要为该引用分配一个新对象:
Vector<Module> listOfModule = new Vector<Module>();
答案 2 :(得分:-1)
我认为问题出在input.next()
here。
public String next()
查找并返回此扫描仪的下一个完整令牌。一个 完整标记之前和之后是匹配的输入 分隔符模式。 此方法可能会在等待输入时阻塞 扫描,即使之前的hasNext()调用返回true。
更新:
消除上述 downvoters input.next()
的混淆来自声明if(input.next() == "MODULE") {