我正在制作ATM,用户必须首先输入其针脚(1234),然后一旦输入正确,用户可以提取50,100或200美元或存入1000美元以下的存款。我的撤销方法都有效,但是当我运行我的程序并尝试存款时,在我输入金额并尝试点击“输入”按钮后没有任何反应。好像我的输入按钮不起作用。以下是用于存款方式的代码:
if (event.getSource() == deposit) {
instructionScreen.setText("Enter the amount you would like to deposit, then click Enter.");
if (event.getSource() == enter) {
saveScreen = displayInput.getText();
double add = Double.parseDouble(saveScreen);
if (add <= 1000) {
balance += add;
instructionScreen.setText("Your new balance is $" + balance + ".");
}
else {
instructionScreen.setText("The maximum amount you can deposit is $1000. Please enter a new amount.");
displayInput.setText("");
if (event.getSource() == enter) {
saveScreen = displayInput.getText();
add = Double.parseDouble(saveScreen);
}
}
displayInput.setText("");
System.out.println("A deposit of $" + add + "was made. Your new balance is $" + balance);
}
}
在我输入存款金额后点击输入时导致我的存款不做任何事情的代码有什么问题?
答案 0 :(得分:2)
如果它是一个线性控制台程序,那么你的程序会很好用,但事实驱动的编程并不是这样。如果选择了存款,可能需要通过更改布尔字段,然后在enter的动作侦听器中测试该字段来改变对象的状态,而不是期望源从存入到输入立即和神奇地改变。
如,
if (event.getSource() == deposit) {
instructionScreen.setText("Enter the amount you would like to deposit, then click Enter.");
depositState = true; // a boolean field
} else if (event.getSource() == enter) {
if (depositState) {
depositState = false; // reset it
saveScreen = displayInput.getText();
double add = Double.parseDouble(saveScreen);
// .... etc