1

我正在制作一个 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

4 回答 4

6

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

ATM myATM = new ATM(); 

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

将相应的行替换为:

private static ATM myATM;

public static void main(String[] arg)
{
    myATM = new ATM();
于 2013-10-09T14:49:43.147 回答
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("****************************************");
    }
    //}
}
于 2013-10-09T14:56:12.383 回答
1

为什么要单独初始化:

ATM myATM = new ATM();

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

于 2013-10-09T14:49:19.893 回答
1

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

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

  1. 您在主目录中创建一个类 ATM 的实例。
  2. JVM 实例化您的类的 atm 对象以创建实例。(代码中的第三个变量)。
  3. 再次调用构造函数。
  4. JVM 尝试创建一个新的 ATM 实例,因此实例化另一个实例变量(返回步骤 2)。

您的代码中有一些错误。您的类中似乎不需要 ATM 对象,您可以使用this关键字访问 ATM 实例变量和工具箱或余额等方法。

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

于 2013-10-09T14:53:59.580 回答