要非常明确的是,我的程序不是简单的等待用户输入。当行
choice = in.nextInt();
我的菜单甚至不会出现,如果我输入任何类型的输入,程序就会被打断。如果没有next.Int()行,这个方法看起来工作得很好,菜单会显示出来,如果我事先手动设置我的选择,我会穿越开关。我甚至尝试在这行前后使用in.nextline(目前已被注释掉)来清除挂起的回车,但这似乎也没有帮助。
我不知道为什么这一行似乎会在我的代码中造成问题。
为了了解更多的背景,这里是 nextInt()给我带来问题的方法。
public void Manage() throws ListException {
Scanner in = new Scanner(System.in);
System.out.println("1. Display items");
System.out.println("2. Add item");
System.out.println("3. Insert item");
System.out.println("4. Delete item");
System.out.println("5. Exit");
System.out.println("Enter option:");
//int choice = in.nextInt();
choice = in.nextInt();
//in.nextLine();
switch (choice) {
//display
case 1:
System.out.println(dList);
break;
//add
case 2:
System.out.println("Please enter the address:");
String a = in.nextLine();
System.out.println("Please enter the square footage:");
double f = in.nextDouble();
System.out.println("Please enter the value:");
double v = in.nextDouble();
try {
Dwelling x = new Dwelling(a,f,v);
dList.add(x);
}
catch (ListException e) {
throw new ListException("add");
}
break;
//insert
case 3:
System.out.println("Please enter the address:");
String ad = in.nextLine();
System.out.println("Please enter the square footage:");
double fo = in.nextDouble();
System.out.println("Please enter the value:");
double va = in.nextDouble();
int p = in.nextInt();
try {
Dwelling y = new Dwelling(ad,fo,va);
dList.insert(y,p);
}
catch (ListException e) {
throw new ListException("insert");
}
System.out.println("Please enter the position to insert:");
break;
//delete
case 4:
System.out.println("Please enter the position to delete:");
int d = in.nextInt();
try {
dList.delete(d);
}
catch (ListException e) {
throw new ListException("delete");
}
break;
//exit
case 5:
break;
}
//use update method to update data file
}//end of manage method