gpt4 book ai didi

用于银行帐户交易的 Java Math.max

转载 作者:行者123 更新时间:2023-12-02 07:32:39 25 4
gpt4 key购买 nike

所以,我一直在解决这个问题,目标是为银行帐户中超过分配的免费交易数量的每笔交易扣除费用。到目前为止,我已经准备好了计算交易的一切,但我们应该与 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;
}
}

最佳答案

max(double, double) 返回灌浆器 double 值。只需更改

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

return Math.max(transactionCount, freeTransactions);

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

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

关于用于银行帐户交易的 Java Math.max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com