gpt4 book ai didi

java - 我的程序中出现 IllegalStateException?

转载 作者:行者123 更新时间:2023-12-01 19:54:01 27 4
gpt4 key购买 nike

Stacktrace Here

导入java.util.*;

公共(public)类 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,在我的情况下是什么导致了它,以及如何解决这个问题?谢谢。

最佳答案

这相当简单,捕获异常后 finally子句被执行。不幸的是,您将在本条款中关闭扫描仪和 Scanner.close()关闭底层输入流(在本例中为System.in)。

标准输入流System.in一旦关闭就无法再次打开。

要解决此问题,您必须省略finally子句并在程序需要终止时而不是更早时关闭扫描器。

关于java - 我的程序中出现 IllegalStateException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50246712/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com