做,而循环。无法输出循环的内容

时间:2015-11-12 13:37:55

标签: c# loops while-loop counter

我是编程新手,我试图在C#中运行程序。我需要的是让用户输入他们当前的银行余额,他们需要的金额和利率。我希望输出显示每年的余额以及达到所需金额所需的时间。谢谢你的帮助。以下是我已经拥有的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interest
{
    class Program
    {
        static void Main(string[] args)
        {
            int time = 1;//1 means 1 year
            int count = 0;
            int num = 0;

        Console.WriteLine("Enter current balance:");
        double Bal = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter required balance:");
        double required = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter interest rate: %");
        double IR = double.Parse(Console.ReadLine());

        double TotalAmount = Bal * IR / 100 * time + Bal;//math to work out interest gained

        Console.WriteLine("Your balance at the end of year 1 is {0:C}", TotalAmount);//this shows the interest gained over 1 year

        //do
        //{
           // Console.WriteLine(Bal);
           // Bal++;
       // } while (Bal < required);

        //I need the code to keep looping until the bal is the same as required
        do {
            TotalAmount =+ count;
        count++;

        } while(TotalAmount < required);
        Console.WriteLine("It took {0} years to get to {1:C}",count, required);

        Console.ReadLine();
    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要增加循环中的时间并根据新时间重新计算TotalAmount。

您不需要使用计数变量。

        int time = 1;//1 means 1 year

        int num = 0;

        Console.WriteLine("Enter current balance:");
        double Bal = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter required balance:");
        double required = double.Parse(Console.ReadLine());

        Console.WriteLine("Enter interest rate: %");
        double IR = double.Parse(Console.ReadLine());

        double TotalAmount = Bal * IR / 100 * time + Bal;//math to work out interest gained

        Console.WriteLine("Your balance at the end of year 1 is {0:C}", TotalAmount);//this shows the interest gained over 1 year

        //do
        //{
        // Console.WriteLine(Bal);
        // Bal++;
        // } while (Bal < required);

        //I need the code to keep looping until the bal is the same as required
        do
        {
            time++;
            TotalAmount = Bal * IR / 100 * time + Bal;//math to work out interest gained

        } while (TotalAmount < required);
        Console.WriteLine("It took {0} years to get to {1:C}", time, required);