【发布时间】:2020-12-29 14:47:38
【问题描述】:
我对编程还很陌生,当我尝试处理 InputMisMatch 异常时,我的程序不断循环出现问题。
我有一个菜单,它接受用户输入,然后使用 switch 语句来处理输入。我试图让我的程序处理两个异常。首先是确保输入实际上是一个整数,其次是确保整数在菜单的范围内。我在 try 块中使用了 NextInt() 方法。在 try 块中是 switch 语句,它使用默认情况来处理不在范围内的输入
但是,当用户输入不是整数的输入时,catch 块会不断循环。似乎我没有在某个地方更新循环内的用户输入,但我不知道在哪里更新它。
目前,我并不关心 switch case 中的内容(尽管我也想在它们内部实现相同的循环功能),这只是外部循环的逻辑。
这是我的代码:
任何指导将不胜感激。谢谢!
public class Main {
// set up scanner for user input
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
// welcome message
welcome();
//declare user choice variables
int menuSelection;
int discSelection;
boolean isInputValid = true ;
do { // keep looping until user input is valid
try {
/*
We can expect an error from user input
1. Input is not an integer
2. Input is not in range
*/
isInputValid = true;
displayMainMenu();
menuSelection = keyboard.nextInt();
System.out.println(); //print new line
//Menu Logic
switch (menuSelection) {
case 0: { // Exit Application
break;
}
case 1: { // Search
searchDiscMenu(); //Display the search menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.searchMusic()");
} else if (discSelection == 2) {
System.out.println("Disc.Game.searchGame()");
}
break;
}
case 2: { // Add
addDiscMenu(); //Display add menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.addMusic()");
} else if (discSelection == 2) {
System.out.println("Disc.Game.addGame();");
}
break;
}
case 3: { // Remove
removeDiscMenu(); //Display remove menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.removeMusic();");
} else if (discSelection == 2) {
System.out.println("Disc.Game.removeGame();");
}
break;
}
case 4: { // View
viewDiscMenu(); //Display view menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.viewMusic();");
} else if (discSelection == 2) {
System.out.println("Disc.Music.viewMusic();");
}
break;
}
case 5: { // Sort
sortDiscMenu(); //Display sort menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.viewMusic();");
} else if (discSelection == 2) {
System.out.println("Disc.Music.viewMusic();");
}
break;
}
case 6: { // Write
writeDiscMenu(); //Display write menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.viewMusic();");
} else if (discSelection == 2) {
System.out.println("Disc.Game.writeGameFile();");
}
break;
}
case 7: { // Read
readDiscMenu(); //Display read menu
discSelection = keyboard.nextInt(); // get user choice
if (discSelection == 1) {
System.out.println("Disc.Music.readMusicFile();");
} else if (discSelection == 2) {
System.out.println("Disc.Game.readGameFile();");
}
break;
}
default: { // Handle exception
isInputValid = false;
System.out.println("Error: Selection Not In Range"); // If the input is an int but not in range
break;
}
}
} catch (InputMismatchException e) { // Handles user typing in a char, double etc. instead of int
isInputValid = false;
System.out.println("Error: Unrecognised Input");
}
} while (!isInputValid);
// Exit application safely
System.out.println("Finished"); // temporary message
}
【问题讨论】:
标签: java exception switch-statement do-while inputmismatchexception