无限的利息计算?利息

2023-09-03 08:00:47 作者:呐。谁的眼泪(り丶我心疼

感谢劳伦斯·伯克在我的其他问题的isMaybeMoney功能,我能够确定输入是否是有钱没钱。

我在做什么,现在正试图计算利息后的总,但我不断收到无限写到屏幕。在世界上什么是错我interestsaccrued功能?它应该是$ 3,522.55,当我用$ 1,234,与3.5%的利息开始平衡。

有人可以帮我吗?

 静浮钱;

静态无效的主要()
{
    字符串[] myMaybeBalances = Accounts.GetStartingBalances();

    myIsMaybeMoneyValidator Miimv =新myIsMaybeMoneyValidator();

    ArrayList的利益= Miimv.interestsAccrued(myMaybeBalances);
    的foreach(在利益对象的利益)
    {
        Console.WriteLine(利息);
    }

    到Console.ReadLine();
}

公众的ArrayList interestsAccrued(字符串[] myMaybeBalances)
{
    ArrayList的利益=新的ArrayList();
    的foreach(在myMaybeBalances串myMaybeBalance)
    {
        布尔myResult中= isMaybeMoney(myMaybeBalance);
        如果(myResult中==真)
        {
            十进制[] =率Accounts.GetRates();

            的for(int i = 0; I< rates.Length;我++)
            {
                十进制率=利率[I]
                总浮= 1;

                INT n_X_t = 360;
                而(n_X_t!= 0)
                {
                    率=(1 +评分[I] / 12);
                    浮动myRate;
                    float.TryParse(rate.ToString(),出myRate);

                    总=总* myRate;
                    总=总*钱;
                    n_X_t = n_X_t  -  1;
                }
                interests.Add(总);
            }
        }
    }
    返回的利益;
}

公共BOOL isMaybeMoney(对象theirMaybeMoney)
{
    字符串myMaybeMoney = theirMaybeMoney.ToString();

    浮动NUM;
    BOOL的isValid = float.TryParse(myMaybeMoney,
    NumberStyles.Currency,
    CultureInfo.GetCultureInfo(EN-US),//缓存
    出NUM);

    钱= NUM​​;
    返回的isValid;
}
 

解决方案

您通过率通过while循环的每一步,这似乎不够合理乘以总的,但你也可变钱的价值乘以总,其中据我可以告诉是开始平衡。

所以,你的天平开始繁殖360倍。如果只有我的储蓄账户的工作就像那个!我不知道如果逻辑的其余部分是正确的,但一开始,尝试移动

 总=总*钱;
 
美式看涨个股期权的功能

是线下

 浮法总量= 1;
 

(或更好,但刚刚从

修改

 浮法总量= 1;
 

 浮法总量=钱;
 

和摆脱线

 总=总*钱;
 

共)

Thanks to Laurence Burke in my other question for the isMaybeMoney function, I am able to determine whether an input is money or not.

What I'm doing now is trying to calculate the total after interest but I keep getting Infinity written to the screen. What in the world is wrong with my interestsaccrued function? It's supposed to be $3,522.55 when I use $1,234 as the starting balance with 3.5% interest.

Can someone please help me out?

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;
}

解决方案

You are multiplying total by the rate each step through the while loop, which seems reasonable enough, but you also multiply total by the value of the variable "money", which as far as I can tell is the starting balance.

So you multiply by the starting balance 360 times. If only my savings accounts worked like that! I'm not sure if the rest of the logic is correct, but for a start, try moving the

total = total * money;

to be under the line

float total = 1;

(or better yet just change from

float total = 1;

to

float total = money;

and get rid of the line

total = total * money;

altogether)