我有一个家庭作业,要创建一个带有循环菜单的类来管理汽车队列。我们在最后一节课上学习了队列。
我的菜单工作得很好,直到它捕捉到InputMismatchException或QueueEmptyException,之后它就会进入无限循环,甚至不会在userInput.nextInt();停止。当它捕捉到QueueFullException而不是其他的时候,它就能工作了。
我的代码是:
import java.util.*;
public class CarQueueManagement {
public static void main(String[] args) throws InputMismatchException, QueueFullException{
ArrayQueue queue = new ArrayQueue(3);;
Scanner userInput = new Scanner(System.in);
int carNum;
int choice = 0;
queue.add(1);
OUTER:
while (true) {
try{
System.out.println("ΜΕΝΟΥ:\n\t1. Άφιξη αυτοκινήτου");
System.out.println("\t2. Αναχώρηση αυτοκινήτου\n\t3. Κατάσταση ουράς\n\t4. Έξοδος");
System.out.print("\n\tΕπιλογή (1-4): ");
choice = userInput.nextInt();
switch (choice){
case 1:
System.out.print("\n\tΆφιξη αυτοκινήτου:\n\t\tΑριθμός Αμαξιού");
carNum = userInput.nextInt();
queue.add(carNum);
break;
case 2:
if(queue.isEmpty()){
System.out.println("\n\tΗ ουρά είναι άδεια, δεν χριάζεται διαγραφή.\n\n");
break;
}
String answer;
while(true){
System.out.print("\n\tΑναχώρηση αυτοκινήτου\n\t\tΕπιβεβαίωση; (y/n): ");
answer = userInput.next();
if(answer.equals("y")){
queue.remove();
break;
}
else if(answer.equals("n"))
break;
}
break;
case 3:
System.out.println("\n\tΚατάσταση ουράς:");
if(queue.isEmpty()) System.out.println("\t\tΗ ουρά είναι άδεια.\n\n");
else if(queue.isFull()) System.out.println("\t\tΗ ουρά είναι γεμάτη.\n\n");
else System.out.println("\t\tΗ ουρά έχει άδιες θέσοις.\n\n");
break;
case 4:
System.out.print("\n\nΕξοδος");
break OUTER;
default:
break;
}
}catch (InputMismatchException exc){
System.out.println("\t\tΛΑΘΟΣ ΕΙΣΑΓΩΓΗ\n");
}catch(QueueEmptyException exc){
System.out.println("\t\t" + exc.getMessage() + "\n");
}catch(QueueFullException exc){
System.out.println("\t\t" + exc.getMessage() + "\n");
}
}
}
}发布于 2016-10-30 01:09:42
来自java.util.Scanner docs的介绍部分(重点是我的):
当扫描器抛出
InputMismatchException时,扫描器将不会传递导致异常的令牌,因此可以通过某种其他方法检索或跳过它。
没有详细信息,您的while(true)循环是:
while (true) {
try{
choice = userInput.nextInt();
switch (choice){
case 1:
...
}
} catch (InputMismatchException exc){
// Do nothing.
}
}当用户输入无法转换为整数的内容时,Scanner会抛出一个InputMismatchException,您可以捕获并忽略它。然后,while循环返回顶部,在那里它尝试执行userInput.nextInt()...但是Scanner仍在查看相同的无效输入,因此它会立即抛出另一个InputMismatchException,您可以再次捕获并忽略该and。在while循环的顶部继续执行,在那里它再次调用nextInt() ...这样的循环将永远持续下去。
您必须强制Scanner跳过错误的输入,因此您的catch代码块应该如下所示:
}catch (InputMismatchException exc){
System.out.println("\t\t[chastise the user in Greek]\n");
userInput.next(); // Skip invalid input.
}其他建议
一般来说,许多小方法比一个大方法更容易理解。嵌套的while循环和switch语句尤其难以理解。我只能通过将这个巨大的main方法分解成许多较小的私有静态方法来发现错误。
至少,每个菜单项都可以用它自己的方法来处理。我还去掉了break标签,将整个菜单放入一个单独的方法中,该方法返回一个指示用户是否完成的boolean。这将main中的整个循环减少为:
boolean done = false;
while (! done) {
try{
done = handleUserInput(queue, userInput);
} catch (InputMismatchException exc) {
System.out.println("\nINPUT ERROR\n");
userInput.next();
} // Other catch blocks as before...
}我的handleUserInput不会做太多事情-它获取用户输入,确定哪个方法应该处理该输入,然后返回true或false...它也可以变得比这更简单。
private static boolean handleUserInput(
final ArrayQueue queue,
final Scanner userInput
) {
boolean done = false;
printMenu();
int choice = userInput.nextInt();
switch (choice) {
case 1:
addToQueue(queue, userInput);
break;
case 2:
removeFromQueue(queue, userInput);
break;
case 3:
displayQueue(queue);
break;
case 4:
printExitMessage();
done = true;
break;
default:
break;
}
return done;
}将不同的菜单活动分成不同的方法使它们更容易理解。例如,当逻辑在main中混合在一起时,很难判断像carNum或answer这样的变量是否是问题的一部分。在这个版本中,carNum是一个被困在addToQueue方法中的局部变量,所以当我在其他地方工作时,我可以完全忽略它。
https://stackoverflow.com/questions/40318776
复制相似问题