gpt4 book ai didi

c# - 解决 C# 利息问题,如何计算储蓄余额达到预期目标余额所需的年数?

转载 作者:行者123 更新时间:2023-12-04 14:50:25 26 4
gpt4 key购买 nike

Instructions

In this exercise you'll be working with savings accounts. Each year, the balance of your savings account is updated based on its interest rate. The interest rate your bank gives you depends on the amount of money in your account (its balance):

  • 3.213% for a negative balance.
  • 0.5% for a positive balance less than 1000 dollars.
  • 1.621% for a positive balance greater or equal than 1000 dollars and less than 5000 dollars.
  • 2.475% for a positive balance greater or equal than 5000 dollars.

You have three tasks, each of which will deal your balance and itsinterest rate.

我解决了前两个任务,要求根据指定的余额计算利率,然后计算考虑利率的年度余额更新。

第三个任务问这个:

Implement the (static) SavingsAccount.YearsBeforeDesiredBalance() method to calculate the minimum number of years required to reach the desired balance:

Example:

SavingsAccount.YearsBeforeDesiredBalance (balance: 200.75m, targetBalance: 214.88m)

Output// 14


到目前为止,我的余额超过 5000 美元的代码:

public static int YearsBeforeDesiredBalance(decimal balance, decimal targetBalance)
{
while ( balance <= targetBalance && targetBalance >5000 )
{
return (int)((0.02475m * balance ) + balance);
balance ++;
}
}

我假设我必须找到一种方法来计算此 while 循环执行的次数,但我一直不知道如何去做。任何指导将不胜感激。

最佳答案

您必须利用您已经定义了以下方法:

decimal CalculateTheInterestRateBasedOnTheSpecifiedBalance(decimal balance) { }
decimal CalculateTheAnnualBalanceUpdateTakingIntoAccountTheInterestRate(decimal balance, decimal interestRate) { }

然后就变得简单了:

public static int YearsBeforeDesiredBalance(decimal balance, decimal targetBalance)
{
if (balance <= 0) throw new ArgumentOutOfRangeException("Balance cannot be less than or equal to zero.");
if (targetBalance < 0) throw new ArgumentOutOfRangeException("Target Balance cannot be less than to zero."); /* Yes, only `<` */
int years = 0;
while (balance < targetBalance)
{
decimal interestRate = CalculateTheInterestRateBasedOnTheSpecifiedBalance(balance);
balance = CalculateTheAnnualBalanceUpdateTakingIntoAccountTheInterestRate(balance, interestRate);
years++;
}
return years;
}

关于c# - 解决 C# 利息问题,如何计算储蓄余额达到预期目标余额所需的年数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69155452/

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