0

所以,我有这个问题,我一直在研究,目标是为超过分配的免费交易数量的银行账户中的每笔交易扣除费用。到目前为止,我已经准备好计算交易数量,但我们应该与 Math.max 合作,以便当您超过免费交易金额时,费用开始从余额中扣除帐户。我在 deductMonthlyCharge 方法中使用 Math.max。我想我知道如何使用 if 和 else 语句来做到这一点,但是我们不允许在这里使用它们,而且我对 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;
}
 }
4

2 回答 2

2

max(double, double)返回 grouter 双精度值。只是改变

Math.max(transactionCount, freeTransactions);
    return transactionCount;

return Math.max(transactionCount, freeTransactions);

如果您希望返回更大的值。

双打,就像所有原始类型一样,没有像对象那样的引用。您需要double foo = functionThatReturnPrimitiveDouble()像我在上面的示例中那样保存返回的值,或者只是再次返回它。

于 2012-10-03T20:15:54.393 回答
0

我认为您想要这样的东西(假设每笔交易超过允许金额的费用为 1.00 美元):

public double deductMonthlyCharge()
{
    int transCount = Math.max(transactionCount, freeTransactions);
    double fee = 1.00 * (transCount - freeTransactions);
    return fee;
}

如果客户没有超过他们允许的免费交易次数,那么(transCount - freeTransactions)将为 0,因此不会收取任何费用。

这段代码本身就有点太聪明了,但我认为这是古怪的要求(不要使用 if 语句,而是使用 max )所要求的。

更清晰(但等效)将是:

public double deductMonthlyCharge()
{
    if (transactionCount > freeTransactions) {
        return 1.00 * (transactionCount - freeTransactions);
    }
    return 0.0;
}
于 2012-10-03T20:22:54.863 回答