java经典题丨一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

题意分析:
解题分析:球走过的总距离,为 n=n+n/2 sum=100(1)+100/2(2)+100/4(3)+100/6(4) +······
知识点:while循环,
h=height 表示高度 h=h/2
d=distance 表示距离

public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		System.out.println("请输入数字n");
		int n = sc.nextInt();

		double h= 100;
		double d = 100;
		int i=0;
		
		while(i<n) {
			d=d+h; // 表示距离
			h=h/2; // 表示高度
			i++;
		}
		
		System.out.println("高度为" + d);
		System.out.println("距离为" + h);

	}

人生格言:不要指望事情会更容易,只能指望自己更强大

猜你喜欢

转载自blog.csdn.net/qq_42960881/article/details/82787917