for练习 兔子

static void Main(string[] args)
{
while (true)
{
int a, b, c, d,e;
Console.WriteLine("请输入养兔子的月份:");
a = Convert.ToInt32(Console.ReadLine());
b = 1;
c = 0;
d = 0;

进入for循环,刚开始以为执行if中语句与else中语句是不牵扯的。嵌套在for循环中,整个if else是不断循环的,根据if的控制条件,先执行if或者else,如果其中有变量值的变化,可以赋值给后执行的部分。比如先执行的if中语句,其中的变量发生了变化,这个变化会赋值到else中继续循环,知道结束循环状态。
for (int i = 1; i <=a; i++)                                              
{ 
if (i == 1)                                                                   
{
Console.WriteLine("幼兔一对");                                        
Console.WriteLine("小兔0对");
Console.WriteLine("成兔0对");
}
else
{
d = d + c;                                                                           
c = b;
b = d;

e = d + c + d;
Console.WriteLine("第" + i + "个月份幼兔的对数是" + b);
Console.WriteLine("第" + i + "个月份小兔的对数是" + c);
Console.WriteLine("第" + i + "个月份成兔的对数是" + d);
Console.WriteLine("第" + i + "个月份兔子的对数总共是" + e);
}
}
Console.ReadLine();
}
}

原文地址:https://www.cnblogs.com/qiaoyifan3111/p/4552663.html