我一直在编写一个银行业务计划,但是我在撤销后改变现有余额时遇到了问题。它第一次工作,但第二次回到原始数量。如果我有300美元,然后提取50美元,它将显示250美元。然而,在它第二次执行并且我提取20美元之后,它将显示我的余额是280美元而不是230美元
我的程序需要有两种方法加上main
方法,我目前的帐户余额为300美元。它还需要一个主菜单。
我的老师说我们不能使用数组,因为我们还没有讨论它。
这是我第一次使用的不仅仅是main
方法,因此我选择的方法可能是错误的。
import java.util.Scanner;
public class BankingATM {
public static void main(String[] args) {
double withdraw = withdrawAmount();
double myBalance = withdrawAmount();
balanceAmount(withdraw, myBalance);
}
public static double withdrawAmount() {
Scanner keyboard = new Scanner(System.in);
int option;
double myBalance;
double withdraw = 0;
myBalance = 300 - withdraw;
do {
System.out.println("Please select an option:");
System.out.println("Press 1 the withdraw from you account.");
System.out.println("Press 2 to check your balance");
System.out.println("Press 3 to Exit");
{
option = keyboard.nextInt();
if (option == 1) {
System.out.println("User selected to withdraw");
System.out.println("How much would you like to withdraw?");
withdraw = keyboard.nextDouble();
myBalance = 300;
double newBalance = myBalance - withdraw;
{
if (withdraw > 500) {
System.out.println("Sorry but the ATM has a limit of $500.00");
} else if (withdraw > myBalance && withdraw > 500) {
System.out.println("Sorry your account only has $300.00");
} else if (withdraw > myBalance) {
System.out.println("Sorry your account only has $300.00");
} else if (withdraw <= myBalance) {
System.out.println("The machine is realeasing $" + withdraw);
System.out.println("Your current balance is $" + (myBalance - withdraw));
}
}
}
if (option == 2) {
System.out.println("User selected to check balance");
balanceAmount(withdraw, myBalance);
}
if (option == 3) {
System.out.println("User chose to exit");
System.exit(0);
}
}
} while (option != 1 || option != 2 || option != 3);
return myBalance;
}
public static void balanceAmount(double withdraw, double myBalance) {
System.out.println("Your balance is" + myBalance);
}
}
答案 0 :(得分:0)
好吧,无论何时取款,你都要将余额初始化为300:
if(option == 1)
{
System.out.println("User selected to withdraw");
System.out.println("How much would you like to withdraw?");
withdraw = keyboard.nextDouble();
myBalance = 300; // you should remove that line.
此外,如果您打算多次调用withdrawAmount()
,则不应将myBalance
变量声明为该方法中的局部变量,因为每次调用该方法都会重置余额。
答案 1 :(得分:0)
删除此行:
myBalance = 300; // you should remove that line.
答案 2 :(得分:0)
在do .. while循环中,进行以下更改: // myBalance = 300; //删除此行 double myBalance = myBalance - 撤回; //分配新值myBalance