感谢Laurence Burke在我关于isMaybeMoney功能的另一个问题中,我能够确定输入是否是金钱。
我现在正在做的是尝试计算兴趣后的总数,但我不断将无限写入屏幕。我感兴趣的功能在世界上有什么问题?当我使用1,234美元作为3.5%的利息的起始余额时,它应该是$ 3,522.55。
有人可以帮帮我吗?
static float money;
static void Main()
{
string[] myMaybeBalances = Accounts.GetStartingBalances();
myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator();
ArrayList interests = Miimv.interestsAccrued(myMaybeBalances);
foreach (object interest in interests)
{
Console.WriteLine(interest);
}
Console.ReadLine();
}
public ArrayList interestsAccrued(string[] myMaybeBalances)
{
ArrayList interests = new ArrayList();
foreach (string myMaybeBalance in myMaybeBalances)
{
bool myResult = isMaybeMoney(myMaybeBalance);
if (myResult == true)
{
decimal[] rates = Accounts.GetRates();
for (int i = 0; i < rates.Length; i++)
{
decimal rate = rates[i];
float total = 1;
int n_X_t = 360;
while (n_X_t != 0)
{
rate = (1 + rates[i] / 12);
float myRate;
float.TryParse(rate.ToString(), out myRate);
total = total * myRate;
total = total * money;
n_X_t = n_X_t - 1;
}
interests.Add(total);
}
}
}
return interests;
}
public bool isMaybeMoney(object theirMaybeMoney)
{
string myMaybeMoney = theirMaybeMoney.ToString();
float num;
bool isValid = float.TryParse(myMaybeMoney,
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), // cached
out num);
money = num;
return isValid;
}
答案 0 :(得分:1)
你总计乘以while循环中每一步的速率,这似乎足够合理,但你也将总数乘以变量“money”的值,据我所知,这是起始余额。 / p>
所以你乘以起始平衡360次。如果只有我的储蓄帐户就这样工作!我不确定其余的逻辑是否正确,但首先,请尝试移动
total = total * money;
在
之下float total = 1;
(或者更好的是从
改变float total = 1;
到
float total = money;
并摆脱界限
total = total * money;
共)
答案 1 :(得分:0)
您的代码未评估。没有利益的构造循环的好处计算! 这不是必要的,但会引入很多高并发症的风险
以下是您想要使用FUNCTIONARY封装的代码:
static void Main()
{
var interests = new List<decimal>();
foreach (string possibleBalance in Accounts.GetStartingBalances())
foreach (decimal rate in Accounts.GetRates())
{
decimal balance;
if (!decimal.TryParse(possibleBalance, NumberStyles.Currency, CultureInfo.CurrentCulture, out balance))
continue;
decimal interest = CalculateInterestAccrued(balance, rate, 12, 30);
interests.Add(interest);
}
foreach (decimal interest in interests)
Console.WriteLine(interest);
Console.ReadKey();
}
static decimal CalculateInterestAccrued(decimal principal, decimal rate, int compoundsPerYear, int years)
{
return principal * (decimal)Math.Pow((double)(1 + rate / compoundsPerYear), compoundsPerYear * years);
}
谢谢,
PRASHANT:)