Java 考试题正在考急需答案

2024-11-22 16:44:21
推荐回答(2个)
回答1:

public class Test2 {
public static void main(String[] args) {
final double hight = 100;
final double up = 0.5;
final int time = 10;

double lastHight = hight;
double all = 0;
for(int i=1;i<=time;i++){
all+=lastHight+lastHight*up;
lastHight=lastHight*up;
}
System.out.println("第"+time+"次落地后有多高:"+lastHight);
System.out.println("第"+time+"次落地后的整个过程小球经过的距离:"+all);
}
}
运行结果:
第10次落地后有多高:0.09765625
第10次落地后的整个过程小球经过的距离:299.70703125
--精度问题自己看看了

回答2:

package test;
public class ballfall {
public static void main(String[] args){
double high = 100L;
double tmphigh = 0L;
double distance = 0L;
for(int i=1;i<10;i++){
tmphigh = high/2;
high = tmphigh;
distance +=tmphigh*3;
}
System.out.println("distance------->"+distance);
}
}

看了别人的答案我汗颜呐,重新写一下吧
distance------->299.4140625

还是不知道对不对,差不多吧,哈哈