【发布时间】:2021-01-15 02:35:22
【问题描述】:
我有一个简单的银行程序,您可以在其中查看余额、存款和取款。顺便说一下,这是一个循环,因此您可以进行多次交易
char anotherTransact, option;
int balance;
double deposit, withdraw;
do {
System.out.println("\nWelcome to ABC BANK \n");
System.out.println("B - Check for Balance");
System.out.println("D - Make Deposit");
System.out.println("W - Make Withdraw");
System.out.println("Q - Quit");
System.out.print("\nSelect an option : ");
option = scan.next().charAt(0);
balance = 100000;
if ((option == 'B') || (option == 'b')) {
System.out.println("\nYour current balance is " +balance);
}
else if ((option == 'D') || (option == 'd')) {
System.out.print("\nEnter amount to Deposit : ");
deposit = scan.nextDouble();
if (deposit > 1) {
System.out.println("Deposit Transaction is successfully completed.");
double newBalance = 100000 + deposit;
}
else if (deposit > 500000) {
System.out.println("Deposit Amount must not be greater than 500,000");
}
else {
System.out.println("Deposit must be greater than zero");
}
}
else if ((option == 'W') || (option == 'w')) {
System.out.print("\nEnter amount to Withdraw : ");
withdraw = scan.nextDouble();
if (withdraw % 100 == 0) {
System.out.println("Withdrawal Transaction is successfully completed.");
}
else if (withdraw > 150000) {
System.out.println("Withdrawal Amount must not be greater then 150,000");
}
else {
System.out.println("Withdrawal Amount must be greater than zero");
}
}
else {
System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
}
System.out.print("\nWant to Transact another (Y/N?) ");
anotherTransact = scan.next().charAt(0);
}
while ((anotherTransact == 'Y') || (anotherTransact =='y'));
问题是当我选择提款选项时,我不知道它应该如何在下一次银行交易中扣除余额并在余额中添加存款。
例如,我会在银行存入 40 美元。然后进行一项新交易以打开我的余额,该余额应将我存入的 40 美元加起来。我怎样才能在我的余额中添加存入的钱?还有提现的时候怎么扣?
【问题讨论】:
-
您没有正确更新
balance -= withdraw;和`余额+=存款;`。对这种类型的用例使用 switch case。最好在那里使用一个类和定义的方法。在进行交易之前显示成功消息是错误的。此外,缺少一些边缘情况,您也可以包括这些情况。
标签: java if-statement