代码奔腾 · 2012年11月28日 0

使用while循环

static void Main(string[] args)
{
double balance, interestRate, targetBalance;
Console.WriteLine(“What is your current balance?”);
balance = Convert.ToDouble(Console.ReadLine());
//您当前的余额?
Console.WriteLine(“What is your current annual interest rate(in %)?”);
interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
//您目前的年利率?
Console.WriteLine(“What balance would you like to have?”);
targetBalance = Convert.ToDouble(Console.ReadLine());
//您想要的余额?

int totaYears = 0;
while (balance < targetBalance)
{
balance *= interestRate;

//等同于balance=balance*interestRate
++totaYears;
}
Console.WriteLine(“In {0} year{1} you’ll have a balance of {2}.”, totaYears, totaYears == 1 ? “” : “s”, balance);
//当年数大于1时,{1}被“s”替代(year→years),否则为空。
Console.ReadKey();
}

除非注明,本博客所有内容 由 秒速340 创作,采用 知识共享 署名-非商业性使用-禁止演绎 3.0 未本地化版本 许可协议进行许可。