方法一:
float hight = 100;
int count = 0;//定义次数
float distance = 100;//设置下落高度
for (hight = 100; hight / 2 >= 0.01f; count++)//for循环用来计算每次下落后的高度
{
hight /= 2;
distance += hight * 2;//要注意距离是需要高度乘以2
Console.WriteLine("第{0}次落地后弹起高度{1}", count + 1, hight);
}
Console.WriteLine("经过的距离{0:f1}米", distance);
Console.WriteLine("经过{0}次落地", count);
方法二:
int count = 0;
float hight = 100;
float distance = 100;
while (hight / 2 >= 0.01f)//在这里判断高度是否为零
{
hight /= 2;
count++;
distance += hight * 2;
Console.WriteLine("第{0}次落地后弹起高度{1}", count, hight);
}
Console.WriteLine("经过的距离{0:f1}米", distance);
Console.WriteLine("经过{0}次落地", count);