我正在制作一个ATM程序,以使用cmd来编译和运行它来学习Java。该程序编译没有错误,但在运行时仅显示at ATM.(init)(ATM.java:6)

任何帮助将不胜感激!

public class ATM
{
    Toolbox myToolbox = new Toolbox();
    Integer balance;
    ATM myATM = new ATM();

    public static void main(String[] arg)
    {
        ATM myATM = new ATM();
        myATM.go();
        myATM.printBal();
        myATM.mainMenu();
    }

    public void go()
    {
        System.out.println("Welcome to online ATM banking");
        System.out.println("How much do you want in your account?");
        //System.out.println("Enter your number");
        balance = myToolbox.readIntegerFromCmd();
    }

    public void printBal()
    {
        System.out.println("****************************************");
        System.out.println(balance);
        System.out.println("****************************************");
    }

    public void mainMenu()
    {
        Integer selected;
        System.out.println("What do you want to do?");
        System.out.println("1 : Withdraw");
        System.out.println("2 : Deposit");
        System.out.println("3 : Inquire");
        System.out.println("4 : Quit");
        System.out.println("Enter your number");
        selected = myToolbox.readIntegerFromCmd();
        //{ if statement for mainMenu options
        if(selected.equals(1))
        {
            myATM.withdraw();
        }
        else if(selected.equals(2))
        {
            myATM.deposit();
        }
        else if(selected.equals(3))
        {
            myATM.inquire();
        }
        else if(selected.equals(4))
        {
            myATM.quit();
        }
        //} if statement end
    }
    //{ mainMenu methods
    public void withdraw()
    {
        System.out.println("****************************************");
        System.out.println("Withdraw");
        System.out.println("****************************************");
        Integer takeOut;
        System.out.println("How much do you want to withdraw?");
        takeOut= myToolbox.readIntegerFromCmd();
        balance = balance - takeOut;
        myATM.printBal();
        myATM.mainMenu();
    }

    public void deposit()
    {
        System.out.println("****************************************");
        System.out.println("Deposit");
        System.out.println("****************************************");
        Integer putIn;
        System.out.println("How much do you want to deposit?");
        putIn = myToolbox.readIntegerFromCmd();
        balance = balance + putIn;
        myATM.printBal();
        myATM.mainMenu();
    }

    public void inquire()
    {
        System.out.println("****************************************");
        System.out.println("Inquire");
        System.out.println("****************************************");
        //why am I making a method that is already written
        myATM.printBal();
        myATM.mainMenu();
    }

    public void quit()
    {
        System.out.println("****************************************");
        System.out.println("Thanks for using the fail ATM");
        System.out.println("****************************************");
        System.out.println(balance);
        System.out.println("****************************************");
        System.out.println("Goodbye");
        System.out.println("****************************************");
        System.exit(0);
    }
    //}
}

最佳答案

无论如何,问题可能出在主要方法之前的第4行(我认为)。

ATM myATM = new ATM();


这是在每个ATM实例中实例化一个ATM并引起它们的无限循环,最终将引发StackOverflowException。但是要走很长一段路。

将相应的行替换为:

private static ATM myATM;

public static void main(String[] arg)
{
    myATM = new ATM();

关于java - Java ATM程序仅在ATM上打印。(init)(ATM.java:6),但编译时没有错误(不使用程序中的任何数组),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19275151/

10-12 05:44