简体   繁体   English

我正在处理的利息计算任务没有 output 正确的结果

[英]An interest calculation task I'm working on doesn't output the proper results

So I'm currently taking a C# fundamentals course as the entrance stage of a 6 month C# learning program.所以我目前正在学习 C# 基础课程作为 6 个月 C# 学习计划的入门阶段。 One of the tasks we have under the "Data Types and Variables" is an interest calculator.我们在“数据类型和变量”下的任务之一是利息计算器。 The premise is such: You have a "deposit" that you write in the first line.前提是这样的:你有一个你写在第一行的“定金”。 The deposit has a 5% interest to it.存款有 5% 的利息。 The program needs to output the deposit's value for 3 years, for each year, all at once.该程序需要 output 3 年的存款价值,每年一次。 So 100.00, 105.00, 110.25, 115.76.所以 100.00、105.00、110.25、115.76。 The numbers after the decimal point need to be rounded to 2 digits, as well as "The rules of math for rounding apply here".小数点后的数字需要四舍五入到 2 位数,以及“此处适用四舍五入的数学规则”。

double deposit = double.Parse(Console.ReadLine());
            double interest = (deposit*5)/100;

            double yearOne = deposit + interest;
            double yearTwo = yearOne + interest;
            double yearThree = yearTwo + interest;

            string totalSums = ($"{yearOne:N2}\n{yearTwo:N2}\n{yearThree:N2}\n");
            
            Console.WriteLine( totalSums );

This is the code I've written so far.这是我到目前为止编写的代码。 It SEEMS to work, but it's not as accepted.它似乎可以工作,但没有被接受。 If I put the deposit value as 100 (one of the examples), I get this output: 100 105.00 110.00 115.00如果我将存款价值设为 100(示例之一),我将得到此 output:100 105.00 110.00 115.00

I've put the calculation for the interest percentage as double interest = (deposit*5)/100 , which checks out if I use an external calculator to test it.我已经将利息百分比的计算作为double interest = (deposit*5)/100 ,它会检查我是否使用外部计算器来测试它。 But the program doesn't give the right output. Say if I put 100.23 as the deposit input, I'll get: 105.24, 110.25, 115.26 when I should get 105.24, 110.50, 116.02.但是程序没有给出正确的 output。假设我将 100.23 作为存款输入,我将得到:105.24、110.25、115.26,而我应该得到 105.24、110.50、116.02。 However, a round 100.00 doesn't even display the digits after the decimal point, just rounds them down to the whole number.但是,舍入 100.00 甚至不显示小数点后的数字,只是将它们四舍五入为整数。

I thought the problem comes from using double and not decimal due to floating-point precision issues, so I changed everything to decimal.我认为问题是由于浮点精度问题而使用double而不是decimal ,所以我将所有内容都更改为 decimal。 I still get this exact problem.我仍然遇到这个确切的问题。 Nothing changes.没有什么变化。

I've had other issues that I at least have ideas on where I'm going wrong, but this one has been screwing with me since 3 days.我还有其他问题,我至少知道我哪里出错了,但是这个问题从 3 天开始就一直困扰着我。 So I resorted to some help here.所以我在这里求助于一些帮助。

Why am I not getting the correct outputs?为什么我没有得到正确的输出? Where is my problem coming from?我的问题来自哪里? Is it the math?是数学吗? Or logic?还是逻辑? Maybe I'm not using the correct method?也许我没有使用正确的方法? I'm at a loss... And the learning resources I've been given don't seem to help me with this issue, too.我不知所措......而且我获得的学习资源似乎也无法帮助我解决这个问题。 Like it's the ONLY task that the resources don't help with.就像这是资源无济于事的唯一任务。

I've also seen other posts about compound interest, but they use arrays and other things that I'm not yet at the point of learning within the program.我也看过其他关于复利的帖子,但他们使用 arrays 和其他我还没有在程序中学习的东西。 So code like that isn't gonna pass the automatic tester.所以这样的代码不会通过自动测试器。 I'm not asking for a complete code solving or "cheat" if you will;如果您愿意,我不是要完整的代码解决或“作弊”; I just need some guidance on what my issue here is, because I'm clueless at this point.我只需要一些关于我的问题的指导,因为我现在一无所知。

Your question is about compound interest, not simple interest.你的问题是关于复利,而不是单利。 Therefore, you need to calculate the new interest every year.因此,您需要每年计算新的利息。

double deposit = double.Parse(Console.ReadLine());

double yearOne = deposit + (deposit * 5)/100;
double yearTwo = yearOne + (yearOne * 5)/100;
double yearThree = yearTwo + (yearTwo * 5)/100;

string totalSums = ($"{yearOne:N2}\n{yearTwo:N2}\n{yearThree:N2}\n");
            
Console.WriteLine( totalSums );

If you know about loops, you can create a loop over 3 years and update the amount in the account:如果您了解循环,您可以创建一个超过 3 年的循环并更新帐户中的金额:

static class Program
{
    static void Main(string[] args)
    {
        // set interest rate (fixed)
        decimal rate = 5m/100;
        // get deposit amount from user
        Console.WriteLine("Enter Initial Amount:");
        string input = Console.ReadLine();
        decimal amount = decimal.Parse(input);
        // loop over three years
        Console.WriteLine($"{"year"}\t{"amount"}");
        for (int year = 1; year <= 3; year++)
        {
            // calculate interest for the year based on 
            // current amount in the account
            decimal interest = rate * amount;
            // deposit interest in account
            amount += interest;
            Console.WriteLine($"{year}\t{amount:c2}");
        }
    }
}

with output与 output

Enter Initial Amount:
1000
year    amount
1       $1,050.00
2       $1,102.50
3       $1,157.63

you need to recalculate the interest amount each year.您需要每年重新计算利息金额。

        double deposit = double.Parse(Console.ReadLine());

        //Use deposit to calculate interest
        double yearOneinterest = (deposit*5)/100;
        double yearOne = deposit + yearOneinterest;

        //Use yearOne amount to calculate interest
        double yearTwointerest = (yearOne*5)/100;
        double yearTwo = yearOne + yearTwointerest;

        //Use yearTwo amount to calculate interest
        double yearThreeinterest = (yearTwointerest*5)/100;
        double yearThree = yearTwo + yearThreeinterest;

        string totalSums = ($"{yearOne:N2}\n{yearTwo:N2}\n{yearThree:N2}\n");
        
        Console.WriteLine( totalSums );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM