【问题标题】:Problems Error Checking Code问题错误检查代码
【发布时间】:2018-05-11 20:03:07
【问题描述】:
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 后为用户生成一个帐户。然后他们可以执行各种任务,如查看余额、取款、存款等。我遇到了一个问题虽然有错误检查程序。它编译时没有错误,并且第一次通过循环时,它工作得很好。但是,如果他们点击退出并输入另一个无效 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:

【问题讨论】:

    标签: java exception error-handling exception-handling error-checking


    【解决方案1】:

    我从任何人那里得到的帮助为零,但经过数小时的挫折后,我意识到这是因为我在要求用户选择一个选项后未能输入 input.nextLine()。这导致扫描仪没有被清除,并导致该语句被打印两次,然后它在我开始时的内部循环中被清除。一旦我添加了 input.nextLine(),它就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      相关资源
      最近更新 更多