我想問一個問題。如果金額大於餘額,則必須退還餘額並將餘額設置爲零。我該怎麼做?我嘗試了很多不同的方法來解決這個問題,但它沒有奏效。謝謝你的幫助。如何返回並設置爲零?
public class BankAccount {
double balance;
BankAccount(double openingBalance){
balance=openingBalance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public double withdraw (double amount){
if (balance > amount){
balance -= amount;
}else if (amount > balance || amount == balance){
**return balance;
balance = 0**
}
return amount;
}
}
Driver類
public class Driver {
static BankAccount acc3;
public static void main (String[] args){
BankAccount acc3 = new BankAccount ("Alana","Neil", 5000);
System.out.println("\nName: " +acc3.Name());
System.out.println("Amount: $" +acc3.balance);
acc3.deposit(100);
System.out.println("Deposit Amount: $" +acc3.balance);
System.out.println("Withdrawl Amount: $"+acc3.withdraw(5400));
System.out.println("The New Balance: $" +acc3.balance);
}
}