因此,我遇到了这个问题,我一直在努力实现的目标是扣除银行账户中每笔交易的费用,这笔交易超过了分配数量的免费交易。到目前为止,我已经掌握了一切以计算交易,但我们应该与Math.max合作,以便在您结束免费交易时,费用开始从余额中扣除帐户。我在我的deductMonthlyCharge方法中使用Math.max。我想我知道如何用if和else语句来做到这一点,但我们不允许在这里使用它们,而且我对Math.max也不是很熟悉。所以,如果有人能指出我正确的方向来解决这个问题,那就太好了。谢谢。Java Math.max用于银行账户交易
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private double fee;
private double freeTransactions;
private double transactionCount;
/**
Constructs a bank account with a zero balance
*/
public BankAccount()
{
balance = 0;
fee = 5;
freeTransactions = 5;
transactionCount = 0;
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
transactionCount = 0;
}
public static void main(String [ ] args)
{
BankAccount newTransaction = new BankAccount();
newTransaction.deposit(30);
newTransaction.withdraw(5);
newTransaction.deposit(20);
newTransaction.deposit(5);
newTransaction.withdraw(5);
newTransaction.deposit(10);
System.out.println(newTransaction.getBalance());
System.out.println(newTransaction.deductMonthlyCharge());
}
public void setTransFee(double amount)
{
balance = amount+(balance-fee);
balance = balance;
}
public void setNumFreeTrans(double amount)
{
amount = freeTransactions;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
transactionCount++;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
transactionCount++;
}
public double deductMonthlyCharge()
{
Math.max(transactionCount, freeTransactions);
return transactionCount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
你会意识到,'Math.max()'返回一个值,对不对?这不会改变你传递给它的任何东西。 – NullUserException
钱不应该创建或丢弃。钱只能转移。 –