我正在尝试构建一个简单的银行账户程序,该程序从余额中减去提款金额,但是当我调用dept方法时,它并没有进行减法。
如何让这个程序工作,我不确定是否应该让dept方法无效或让它返回一个值。
import java.util.Scanner;
public class JavaApplication7 {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
Account account1 = new Account( 50.0 );
System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
double withdrawalAmount;
System.out.print( "Enter withdrawal amount for account1: " );
withdrawalAmount = input.nextDouble();
System.out.printf( "\nsubtracting %.2f from account1 balance\n", withdrawalAmount );
account1.dept(withdrawalAmount);
System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
}
}
public class Account {
private double balance; // instance variable that stores the balance
public Account( double initialBalance )
{
if ( initialBalance > 0.0 )
balance = initialBalance;
}
public double dept (double dept1){
dept1=balance-dept1;
return dept1;
}
public double getBalance()
{
return balance;
}
}