检查代码时出现问题

时间:2018-05-11 20:03:07

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

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:

1 个答案:

答案 0 :(得分:0)

我没有得到任何人的帮助,但是经过几个小时的挫折之后我意识到这是因为在我要求用户选择之后我没有输入input.nextLine()。这导致扫描器不被清除,导致语句在开始时在内环中被清除之前被打印两次。一旦我添加了input.nextLine(),它就可以了。