[英]Problems Error Checking Code
import java.util.*;
public class AccountClient {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
boolean infiniteLoop = true;
boolean invalidInput;
int id = 0;
// Create array of different accounts
Account[] accountArray = new Account[1000000];
//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("name", i, 100);
}
do {
try {
//inner loop to detect invalid Input
do {
invalidInput = false;
System.out.print("Enter an id: ");
if (!(input.hasNextInt())) {
System.out.println("Invalid input. Please enter a numeric id between 0 and 999999, no letters or symbols allowed. Try again.");
invalidInput = true;
input.nextLine();
}
else {
id = input.nextInt();
accountArray[id].setNumberOfTimesOpened(accountArray[id].getNumberOfTimesOpened() + 1);
input.nextLine();
if (accountArray[id].firstTimeAccount()) {
System.out.print("Please enter a name to register to this account: ");
String name = input.nextLine();
accountArray[id].setName(name);
}
}
} 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: view transaction history\n5: exit\nEnter a choice: ");
choice = input.nextInt();
if (choice < 1 || choice > 5) {
System.out.println("Sorry, " + choice + " is not an option. Please try again and enter a number between 1 and 5 (inclusive).");
notAnOption = true;
}
} while(notAnOption);
switch (choice) {
case 1: System.out.printf("The balance for your account is $%.2f\n", accountArray[id].getBalance());
break;
case 2: {
boolean withdrawFlag;
do {
System.out.print("Enter the amount you would like to withdraw: ");
double withdrawAmount = input.nextDouble();
input.nextLine();
if (withdrawAmount > accountArray[id].getBalance()) {
System.out.printf("Sorry, you only have an account balance of $%.2f. Please try again and enter a number at or below this amount.\n", accountArray[id].getBalance());
withdrawFlag = true;
}
else {
accountArray[id].withdraw(withdrawAmount);
System.out.printf("Thank you. You have successfully withdrawn $%.2f from your account.\n", withdrawAmount);
withdrawFlag = false;
}
} while (withdrawFlag);
}
break;
case 3: {
System.out.print("Enter the amount you would like to deposit: ");
double depositAmount = input.nextDouble();
input.nextLine();
accountArray[id].deposit(depositAmount);
System.out.printf("Thank you. You have successfully deposited $%.2f into your account.\n", depositAmount);
}
break;
case 4: {
accountArray[id].accountSummary();
}
break;
case 5: {
System.out.println("returning to the login screen...\n");
exit = true;
}
break;
}
} while (exit == false);
}
catch (ArrayIndexOutOfBoundsException ex1) {
System.out.println("Invalid input. Please enter an id between 0 and 999999 (inclusive).");
input.nextLine();
}
catch (InputMismatchException ex2) {
System.out.println("Sorry, invalid input. Please enter an id between 0 and 999999 (inclusive) with no letters or symbols.");
input.nextLine();
}
} while (infiniteLoop);
}
}
大家好,我有一个模拟ATM机的程序。 它使用我创建的帐户类,在用户输入0到999999之间的ID后为用户生成帐户。然后他们可以执行各种任务,例如查看余额,取款,存款等。我遇到了问题,但出现错误检查程序。 它编译时没有错误,并且在第一次循环时就可以完美运行。 但是,如果他们按exit并输入另一个无效的ID,它将两次显示无效的输入消息。 我复制了下面发生的情况的控制台。 有人可以向我解释为什么这样做以及如何解决它。 我也是java的新手,所以如果有人可以告诉我一种更好的错误检查方法,将不胜感激。 截至目前,如果他们输入一个int值并且它不在0到999999的范围内,我必须有一个单独的ArrayIndexOutofBoundsException来捕获错误。 这似乎效率低下。 有什么方法可以错误检查它们是否输入了数值,是否输入了错误值,然后再次检查是否输入了0到999999之间的输入? 谢谢
输入ID:f
输入无效。 请输入介于0到999999之间的数字ID,不允许使用字母或符号。 再试一次。
输入ID:5
请输入名称以注册到该帐户:Bob
主菜单
1:检查余额
2:退出
3:存款
4:查看交易记录
5:退出
输入选择:5
返回登录屏幕...
输入ID:f
输入无效。 请输入介于0到999999之间的数字ID,不允许使用字母或符号。 再试一次。
输入ID:输入无效。 请输入介于0到999999之间的数字ID,不允许使用字母或符号。 再试一次。
输入ID:
我从任何人那里得到了零帮助,但是经过数小时的挫败之后,我意识到这是因为我在要求用户选择一个选项之后,无法放入input.nextLine()。 这导致扫描器无法清除,并使语句在开始时在内部循环中被清除之前被打印两次。 一旦我添加了input.nextLine(),它就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.