【发布时间】:2018-12-02 20:18:39
【问题描述】:
我为我的 java 类编写的菜单程序遇到了一些问题。运行一个程序后,当程序进行第二次循环时,它会在应该为下一个要运行的程序获取用户输入的行上抛出一个NoSuchElementException。我假设它与扫描仪搞砸有关,但我找不到问题。有人有想法么?
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String pin;
int selection = 0;
boolean valid = false;
do {
System.out.print("Please enter the password: ");
pin = console.nextLine();
valid = checkPassword(pin);
} while (!valid);
while (selection != 4 && valid == true) {
System.out.printf("%nPlease select a number from the menu below %n1: Wage "
+ "Calculator 2: Tip Calculator 3: Grocery Discount 4: Exit %n");
selection = console.nextInt();
if (selection == 1) {
calc_wages();
} else if (selection == 2) {
calc_tip();
} else if (selection == 3) {
System.out.print("We haven't gotten this far yet");
} else if (selection == 4){
System.out.print("Thank you for using the program.");
break;
} else {
System.out.print("There is no option for what you entered. Try again");
}
selection = 0;
}
}//main
【问题讨论】:
-
你能显示
calc_wages();和calc_tip();的代码吗?您是否在这两种方法中的任何时候关闭Scanner? -
请发布堆栈跟踪。
标签: java debugging java.util.scanner nosuchelementexception