do,while循环.无法输出循环的内容
本文关键字:循环 输出 while do | 更新日期: 2023-09-27 18:21:43
我是编程新手,正在尝试用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();
}
}
}
您需要增加循环中的时间,并根据新时间重新计算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);