gpt4 book ai didi

Java ATM 程序只是在 ATM 处打印。(init)(ATM.java :6) but compiles with no errors (not using any arrays in program)

转载 作者:行者123 更新时间:2023-11-29 09:54:18 26 4
gpt4 key购买 nike

我正在制作一个 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);
}
//}
}

最佳答案

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

ATM myATM = new ATM(); 

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

将相应的行替换为:

private static ATM myATM;

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

关于Java ATM 程序只是在 ATM 处打印。(init)(ATM.java :6) but compiles with no errors (not using any arrays in program),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19275151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com