我正在制作一个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);
}
//}
}
答案 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 。
基本上你的代码中有一个循环:
您的代码中存在一些错误。您似乎不需要类中的ATM对象,可以使用 this 关键字访问ATM实例变量以及工具箱或平衡等方法。
因此尝试删除第三个变量myATM。您不需要它并通过 this 替换方法中的myATM(main除外)。