编程语言
首页 > 编程语言> > Java经典练习题10

Java经典练习题10

作者:互联网

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

import java.util.*;

public class Class10 {

public static void main(String[] args) {
int h0 = 100;
int h = 0;
int ht = 0;
int dh = 0;
int h1 = 0;
System.out.println("请输入第几次落下:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n == 1){
h = h0;
ht = h0/2;
System.out.println("共经过的米数:");
System.out.println(h);
System.out.println("反弹的高度:");
System.out.println(ht);
}else if(n <= 0){
System.out.println("输入有误!");
}else{
for(int i = 2; i <= n; i++){
ht = h0/(2*(i - 1));
//System.out.println(ht);
dh = ht*2;
h1 = h1 + dh;
//System.out.println(h1);
}
h = h0 + h1;
System.out.println("共经过的米数:");
System.out.println(h);
System.out.println("反弹的高度:");
System.out.println(ht);
}
}

}

标签:练习题,10,Java,int,h0,System,ht,println,out
来源: https://www.cnblogs.com/zhuozige/p/12357979.html