简体   繁体   中英

do, while loops. Cannot output the contents of the loop

I'm new to programming and I'm trying to run a program in C#. What I need is for the user to enter their current bank balance, the amount they require to have and the interest rate. I want the output to show their balance each year and how many years it would take to reach their required amount. Thanks for your help. Below is the code that I already have:

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

}

You need to increment the time in your loop and recalculate the TotalAmount on the basis of the new time.

You do not need to use the count variable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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