Java ATM程序只打印在ATM。(init)(ATM.java:6)但编译没有错误(不使用程序中的任何数组)

时间:2013-10-09 14:40:21

标签: java

我正在制作一个ATM程序来学习java使用cmd来编译和运行它。该程序编译时没有错误,但在运行时只打印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 个答案:

答案 0 :(得分:6)

问题可能是主要方法之前的第4行(我认为)。

ATM myATM = new ATM(); 

这是在每个ATM实例中实现ATM并导致它们的无限循环,最终会抛出StackOverflowException。但这将是一个漫长的过程。

用以下代码替换相应的行:

private static ATM myATM;

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

答案 1 :(得分:2)

很难看出这是否与您的工具箱一起编译。 但你可以使用它代替myATM并使用while循环而不是调用相同的方法。

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

    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() {
        boolean running = true;
        while (running) {
            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)) {
                this.withdraw();
            } else if (selected.equals(2)) {
                this.deposit();
            } else if (selected.equals(3)) {
                this.inquire();
            } else if (selected.equals(4)) {
                this.quit();
                running = false;
            }
        }
        //} 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;
        this.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;
        this.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
        this.printBal();
        //this.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("****************************************");
    }
    //}
}

答案 2 :(得分:1)

为什么要单独初始化:

ATM myATM = new ATM();

在main()方法的正上方?这似乎是你的问题。

答案 3 :(得分:1)

这里的真正错误是 java.lang.StackOverflowError

基本上你的代码中有一个循环:

  1. 您可以在主体中创建一个ATM类的实例。
  2. JVM实例化您的类的atm对象以创建实例。 (代码中的第三个变量)。
  3. 再次调用构造函数。
  4. JVM尝试创建一个新的ATM实例,因此实例化另一个实例变量(GO回到步骤2)。
  5. 您的代码中存在一些错误。您似乎不需要类中的ATM对象,可以使用 this 关键字访问ATM实例变量以及工具箱或平衡等方法。

    因此尝试删除第三个变量myATM。您不需要它并通过 this 替换方法中的myATM(main除外)。