C#基础:一个球从100米高度落下,每次落地后,弹回原高度的一半;计算总共几次最终落地。总共经过多少米。
作者:互联网
方法一:
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);
标签:count,distance,总共,Console,落地,hight,WriteLine,弹回,100 来源: https://blog.csdn.net/qq_47116241/article/details/121167929