循环,帮助我的循环内的方程! [英] Looping, help with an equation inside of my loop!

查看:69
本文介绍了循环,帮助我的循环内的方程!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在撰写一个计划,以5%的速度显示一个城镇的人口增长率,增长率为4%。

我有我的变量和我的计数器。这就是我所拥有的。

Hello,

I am writing a program to show a population growth for a town over 5 years at a 4% growth rate.
I have my variables and my counter. Here is what I have.

static void Main(string[] args)
      {
          int curntPopulation = 23962;
          int i = 1;
          double growthRate = 1.04;
          double newPopulation;


          while (i <= 5)
          {
              newPopulation = curntPopulation * growthRate;

              newPopulation = newPopulation * growthRate;

              Console.WriteLine("The population of Davidville is: {0}", newPopulation);
              i++;
          }

      }





我遇到的困难是显示5年来每年的新人口,目前我正在显示所有5行的两年结果。如何使newPopulation保存当前值,将其存储在等式中并生成我之后的结果?谢谢



What I am having difficulties with is displaying the new population for each of the 5 years, currently I am displaying the year two results for all 5 lines. How can I make newPopulation save the current value, store it in the equation and generate the result I am after? Thanks

推荐答案

static void Main(string[] args)
        {
            int population = 23962;
            int totalNumberOfYears = 5;
            double growthRate = 1.04;

    Console.WriteLine("The population of Davidville is: {0}", newPopulation);

    for(int year = 1; year < totalNumberOfYears; year++)
    {
        population = population * growthRate;

        Console.WriteLine("The population of Davidville in year {0} is: {1}", year, newPopulation);
    }

}





试试吧,它应该可以胜任。



Try that, it should do the job.


这篇关于循环,帮助我的循环内的方程!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆