【发布时间】:2018-05-09 14:46:28
【问题描述】:
导入 java.util.*;
公共类 AccountClient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean infiniteLoop = true;
boolean invalidInput;
int id;
// Create array of different accounts
Account[] accountArray = new Account[10];
//Initialize each account array with its own unique id and a starting account balance of $100
for (int i = 0; i < accountArray.length; i++) {
accountArray[i] = new Account(i, 100);
}
do {
try {
//inner loop to detect invalid Input
do {
invalidInput = false;
System.out.print("Enter an id: ");
id = input.nextInt();
if (id < 0 || id > 9) {
System.out.println("Try again. Id not registered in system. Please enter an id between 0 and 9 (inclusive).");
invalidInput = true;
input.nextLine();
}
} while (invalidInput);
boolean exit;
do {
exit = false;
boolean notAnOption;
int choice;
do {
notAnOption = false;
System.out.print("\nMain Menu\n1: check balance\n2: withdraw\n3: deposit\n4: exit\nEnter a choice: ");
choice = input.nextInt();
if (choice < 1 || choice > 4) {
System.out.println("Sorry, " + choice + " is not an option. Please try again and enter a number between 1 and 4 (inclusive).");
notAnOption = true;
}
} while(notAnOption);
switch (choice) {
case 1: System.out.println("The balance for your account is $" + accountArray[id].getBalance());
break;
case 2: {
boolean withdrawFlag;
do {
System.out.print("Enter the amount you would like to withdraw: ");
double withdrawAmount = input.nextInt();
if (withdrawAmount > accountArray[id].getBalance()) {
System.out.println("Sorry, you only have an account balance of $" + accountArray[id].getBalance() + ". Please try again and enter a number at or below this amount.");
withdrawFlag = true;
}
else {
accountArray[id].withdraw(withdrawAmount);
System.out.println("Thank you. Your withdraw has been completed.");
withdrawFlag = false;
}
} while (withdrawFlag);
}
break;
case 3: {
System.out.print("Enter the amount you would like to deposit: ");
double depositAmount = input.nextInt();
accountArray[id].deposit(depositAmount);
System.out.println("Thank you. You have successfully deposited $" + depositAmount + " into your account.");
}
break;
case 4: {
System.out.println("returning to the login screen...\n");
exit = true;
}
break;
}
} while (exit == false);
}
catch (InputMismatchException ex) {
System.out.println("Sorry, invalid input. Please enter a number, no letters or symbols.");
}
finally {
input.close();
}
} while (infiniteLoop);
}
}
异常代码:
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at playground.test.main.Main.main(Main.java:47)
您好,我编写了一个基本程序,它使用一个名为 account 的类来模拟 ATM 机。如果用户没有输入字母,我想抛出异常。这很好用,但是我需要让它循环,以便程序在抛出异常后不会终止。为此,我只需将 try catch 放在我之前的 do while 循环中。但是,当我这样做时,每次我输入一个字母或选择退出我拥有的内部循环时都会抛出一个 IllegalStateException ,这会将用户带回到要求他们输入他们的 ID 的循环。什么是 IllegalStateException,在我的情况下是什么原因造成的,我将如何解决这个问题?谢谢。
【问题讨论】:
-
发布 whole 堆栈跟踪并告诉我们发生错误的行!
-
输入 id:0 主菜单 1:检查余额 2:取款 3:存款 4:退出 输入选择:4 返回登录屏幕...线程“main”中的异常输入 id : java.lang.IllegalStateException: 扫描程序在 java.util.Scanner.next(Unknown Source) 在 java.util.Scanner.nextInt(Unknown Source) 在 java.util.Scanner.ensureOpen(Unknown Source) 关闭。 AccountClient.main(AccountClient.java:23) 处的 Scanner.nextInt(Unknown Source)
-
不要将其作为评论发布,而是编辑您的问题!
-
这实际上是我的第一篇文章,所以对于可怕的格式我很抱歉。是否有某种方法可以让我发布堆栈跟踪,或者我只是将其复制并粘贴到我的代码中。
-
不要关闭扫描仪。
标签: java while-loop exception-handling illegalstateexception