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)グラウターの double 値を返します。変えるだけ

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 に答える