【发布时间】:2022-09-23 01:45:27
【问题描述】:
有人可以向我解释为什么在withdraw() 中需要添加return,但在deposit() 中不需要它?
public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public void checkBalance(){
System.out.println(\"Hello!\");
System.out.println(\"Your balance is \" + balance);
}
public void deposit(int amountToDeposit){
balance = balance + amountToDeposit;
System.out.println(\"You just deposited \" + amountToDeposit);
}
public int withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
System.out.println(\"You just withdrew \" + amountToWithdraw);
return amountToWithdraw;
}
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
//Withdrawing:
savings.withdraw(150);
//Deposit:
savings.deposit(25);
//Check balance:
savings.checkBalance();
}
}
标签: java