我做了一个ATM程序。我有一个尝试捕获器,它将要求用户输入其PIN码。密码必须是5位数字。因此,异常将检查它是否为5位数字,但异常处理无效。无论我输入什么数字,它总是显示无效数字。

这是我的代码,try catch在程序的顶部,而异常处理checkNumber在程序的底部。

import java.util.ArrayList;
import java.util.Scanner;

public class BankMain
{
    private double availableBal = 80;
    private double totalBal = 100;
    private double availableBal2 = 480;
    private double totalBal2 = 500;
    private double availableBal3 = 80;
    private double totalBal3 = 100;
    ArrayList<Integer> cardNum = new ArrayList<Integer>();
    static Scanner input = new Scanner(System.in);

    private String error; // String the error from the exception
    {
        error = "error";
    }

    public void cardNumbers()
    {

        Scanner cards = new Scanner(System.in);
        Scanner input = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);

        try
        {

            System.out.println("Please select a 5 digit card number");

            cardNum.add(input.nextInt());
            checkNumber();

        }

        catch (invalidNumber err)
        {

            System.out.println("Caught Error: " + err.getError());
        }

        System.out.println("Thank you! You're card number is " + cardNum);

        System.out.println("Type 'c' to go back to main menu.");
        String value = keyboard.next();

        if (value.equalsIgnoreCase("c"))
        {
            menu();

        }

        else if (!keyboard.equals('c'))
        {

            System.out.println("Invalid Entry!");

        }

    }

    public void menu()
    {

        System.out.println("ATM Menu:");
        System.out.println();
        System.out.println("1 = Create Account");
        System.out.println("2 = Account Login");
        System.out.println("3 = Exit ATM");
        query();
    }

    public void startAtm()
    {
        menu();

    }

    public void drawMainMenu()
    {

        AccountMain main3 = new AccountMain();
        int selection;

        System.out.println("\nATM main menu:");
        System.out.println("1 - View account balance");
        System.out.println("2 - Withdraw funds");
        System.out.println("3 - Add funds");
        System.out.println("4 - Back to Account Menu");
        System.out.println("5 - Terminate transaction");
        System.out.print("Choice: ");
        selection = input.nextInt();

        switch (selection)
        {
        case 1:
            viewAccountInfo();
            break;
        case 2:
            withdraw();
            break;
        case 3:
            addFunds();
            break;
        case 4:
            AccountMain.selectAccount();
            break;
        case 5:
            System.out.println("Thank you for using this ATM!!! goodbye");
        }
    }

    public void viewAccountInfo()
    {
        System.out.println("Account Information:");
        System.out.println("\t--Total balance: $" + totalBal);
        System.out.println("\t--Available balance: $" + availableBal);
        drawMainMenu();
    }

    public void viewAccountInfo2()
    {
        System.out.println("Account Information:");
        System.out.println("\t--Total balance: $" + totalBal2);
        System.out.println("\t--Available balance: $" + availableBal2);
        drawMainMenu();
    }

    public void deposit(int depAmount)
    {
        System.out.println("\n***Please insert your money now...***");
        totalBal = totalBal + depAmount;
        availableBal = availableBal + depAmount;
    }

    public void checkNsf(int withdrawAmount)
    {
        if (totalBal - withdrawAmount < 0)
            System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
        else
        {
            totalBal = totalBal - withdrawAmount;
            availableBal = availableBal - withdrawAmount;
            System.out.println("\n***Please take your money now...***");
        }
    }

    public void addFunds()
    {
        int addSelection;

        System.out.println("Deposit funds:");
        System.out.println("1 - $20");
        System.out.println("2 - $40");
        System.out.println("3 - $60");
        System.out.println("4 - $100");
        System.out.println("5 - Back to main menu");
        System.out.print("Choice: ");
        addSelection = input.nextInt();

        switch (addSelection)
        {
        case 1:
            deposit(20);
            drawMainMenu();
            break;
        case 2:
            deposit(40);
            drawMainMenu();
            break;
        case 3:
            deposit(60);
            drawMainMenu();
            break;
        case 4:
            deposit(100);
            drawMainMenu();
            break;
        case 5:
            drawMainMenu();
            break;
        }
    }

    public void withdraw()
    {
        int withdrawSelection;

        System.out.println("Withdraw money:");
        System.out.println("1 - $20");
        System.out.println("2 - $40");
        System.out.println("3 - $60");
        System.out.println("4 - $100");
        System.out.println("5 - Back to main menu");
        System.out.print("Choice: ");
        withdrawSelection = input.nextInt();

        switch (withdrawSelection)
        {
        case 1:
            checkNsf(20);
            drawMainMenu();
            break;
        case 2:
            checkNsf(40);
            drawMainMenu();
            break;
        case 3:
            checkNsf(60);
            drawMainMenu();
            break;
        case 4:
            checkNsf(100);
            drawMainMenu();
            break;
        case 5:
            drawMainMenu();
            break;
        }
    }

    public void query()
    {

        Scanner keyboard = new Scanner(System.in);
        double input = keyboard.nextInt();

        if (input == 2)
        {

            BankMainPart2 main2 = new BankMainPart2();

            System.out.println("Please enter your 5 digit card number.");
            BankMainPart2.loginCard(cardNum);

        }
        else if (input == 1)
        {

            cardNumbers();

        }

        else if (input == 3)
        {
            System.out.println("Thank you, have a nice day!");
            System.exit(0);

        }

    }

    public void checkingMenu()
    {

        AccountMain main3 = new AccountMain();
        int selection;

        System.out.println("\nATM main menu:");
        System.out.println("1 - View account balance");
        System.out.println("2 - Withdraw funds");
        System.out.println("3 - Add funds");
        System.out.println("4 - Back to Account Menu");
        System.out.println("5 - Terminate transaction");
        System.out.print("Choice: ");
        selection = input.nextInt();

        switch (selection)
        {
        case 1:
            viewAccountInfo2();
            break;
        case 2:
            withdraw();
            break;
        case 3:
            addFunds();
            break;
        case 4:
            AccountMain.selectAccount();
            break;
        case 5:
            System.out.println("Thank you for using this ATM!!! goodbye");
        }
    }

    private static void checkNumber() throws invalidNumber // run the check activation exception
    {
        if (String.valueOf(input).length() != 5)
        {
            throw new invalidNumber("invalid number");
        }
        else
        {

            System.out.println("Works!");
        }

    }

    public static void main(String args[])
    {
        BankMain myAtm = new BankMain();
        myAtm.startAtm();
    }
}

最佳答案

为什么要拥有三个不同的Scanner类实例。这就是使程序令人困惑的原因。

compareNumber()方法中,您实际上是在检查input的值,它是Scanner的一个实例。最好像这样使用它:-

checkNumber(input.nextInt())

并将数字添加到checkNumber(int num)方法的列表中。

当然,我并不是说这是一种很好的编码方式。但是它暂时可以解决您的问题。

否则,您的代码有很多问题。

这是您的try-catch块:-

        try {
            System.out.println("Please select a 5 digit card number");
            cardNum.add(input.nextInt());
            checkNumber();
        } catch (invalidNumber err) {
            System.out.println("Caught Error: " + err.getError());
        }


这是您的checkNumber()方法:-

private static void checkNumber() throws invalidNumber
{
    if (String.valueOf(input).length() != 5) {
        throw new invalidNumber("invalid number");
    }
    else {
        System.out.println("Works!");
    }
}


现在,您必须看到您正在使用input作为String.valueOf(input)的参数。

但是,您在try-catch块之前已将“ input”声明为Scanner的实例。

    Scanner cards = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    Scanner keyboard = new Scanner(System.in);


这段代码在您的codeNumbers()方法中。

因此,很明显,您的input永远不会真正包含用户输入,而是一个表示对象hashcodenew Scanner(System.in)

因此,最好将用户的整数输入传递给checkNumber()方法。

************修改代码中需要。

因此,您的checkNumber()`将被修改为:-

    private static void checkNumber(int number) throws invalidNumber
    {
        if (String.valueOf(number).length() != 5) {
            throw new invalidNumber("invalid number");
        }
        else {
            System.out.println("Works!");
        }
    }


在try-catch块中对该方法的调用将更改为:-

        try {
            System.out.println("Please select a 5 digit card number");
            int number = input.nextInt();
            cardNum.add(number);
            checkNumber(number);
        } catch (invalidNumber err) {
            System.out.println("Caught Error: " + err.getError());
        }

09-07 23:49