题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
import java.util.Scanner; public class testOne { /** * @param args */ private double totalHeight=100; private double curHeight=50; //下落 public void drop(int times){ if((times-1)==0){ return ; } setTotalHeight(getTotalHeight()+2*getCurHeight()); setCurHeight(getCurHeight()/2); drop(times-1); } private void setCurHeight(double CurHeight) { curHeight=CurHeight; } public void setTotalHeight(double TotalHeight) { totalHeight=TotalHeight; } public double getCurHeight() { return curHeight; } public double getTotalHeight() { return totalHeight; } public static void main(String[] args) { System.out.println("请输入次数:"); Scanner sc=new Scanner(System.in); int i=sc.nextInt(); testOne main=new testOne(); main.drop(i); System.out.println("总高度是"+main.getTotalHeight()); System.out.println("最后一次反弹高度是"+main.getCurHeight()); } }
时间: 2024-10-12 16:24:16