Java中的实例化问题
当运行以下程序时,我无法到达主功能的末尾。。。 我是Java新手,找不到它的缺陷。我需要你的帮助。谢谢Java中的实例化问题,java,debugging,instantiation,Java,Debugging,Instantiation,当运行以下程序时,我无法到达主功能的末尾。。。 我是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;
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;
}
}
课程表{
向量列表模块;
/*checkClash:检查otherModule是否与其中一个模块冲突
*课程表中的模块。
*先决条件:
*后条件:
*/
公共布尔校验冲突(模块其他模块){
用于(模块c:listOfModule)
如果(c.clashWith(其他模块))
返回true;
返回false;
}
/*添加:将新模块添加到时间表列表中。
*先决条件:
*后条件:
*/
公共无效添加(模块){
添加(模块);
}
/*计数:计算当天的课程数。
*先决条件:
*后条件:
*/
公共整数计数(字符串日){
int count_day=0;
用于(模块c:listOfModule)
计数\天+=c.计数(天);
返回计数日;
}
}
公共类主{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int-num_操作;
字符串代码;
时间表用户时间表=新时间表();
num_operation=input.nextInt();
对于(int i=0;i找到了它。您正在使用=
来比较字符串
改用这个:
if (input.next().equals("MODULE"))
//...
if(input.next().equals("COUNT"))
还值得一提的是,这不会捕获“Count”、“Count”、“Module”、“Module”或“Module”-您可能需要使用equalsIgnoreCase()
。在时间表
中,您永远不会为模块列表指定值:
Vector<Module> listOfModule;
我认为问题在于input.next()
公共字符串next()
查找并返回此扫描程序中的下一个完整令牌。A
完整标记的前面和后面是与
分隔符模式。此方法在等待输入时可能会阻塞
扫描,即使先前调用hasNext()返回true
更新:
消除对输入的混淆。上面的next()
来自语句if(input.next()=“MODULE”){
这是一个巨大的类(为什么?),或者这些是不同的文件?文件中有三个类我没有收到错误…但是当运行时,在读取两个输入后,运行跳了起来out@PUPIALEX请逐行调试您的程序,找出它的终止位置。并给我们一些示例输入。您提供的输入是什么?但这并不能解释为什么它从未终止。如假设input.nextInt()
是一个有效的整数,那么就不会有任何其他地方无法完全执行。对,但我的观点是,他说“我无法到达主函数的末尾”,而这并不能解释这样无限期执行的代码。这取决于OP是如何定义的“主要职能结束".你和我把它看作是程序终止时。我把它看作是OP看到他们想要的输出时。很可能,给定代码,OP没有看到他们想要的输出,因此他们无法到达main的末尾。我明白了,OP似乎不知道如何有效地传达他正在经历的行为…+1表示明智的直觉。@Makoto请仔细查看if(input.next()=“MODULE”){
我的错误,这是它使用的一个地方。但是,如果它没有阻止并接收到一个值,那么if语句将不会执行,因为这是一个引用相等,而不是值相等比较。@Makoto我回答了这个阅读操作“我无法达到主要功能的终点”,我已经-2哈哈。。,
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));
}
}
}
}
if (input.next().equals("MODULE"))
//...
if(input.next().equals("COUNT"))
Vector<Module> listOfModule;
Vector<Module> listOfModule = new Vector<Module>();