方法:递归;
缺点:虚拟机栈大小的考虑,默认是:1M,
优化内容:递归数据的方式;
代码:
package com.hqw.server;
import lombok.extern.slf4j.Slf4j;
/**
* @desc:球跳起来后,高度为原来的0.5,初始高度为:H,求第N次的跳起的高度;
* @checked: 10 1- 5 2 - 2.5 3- 1.25
*/
@Slf4j
public class TestMoment {
public static double initialHeight = 100.0;
public static final double JUMP_THRESHOLD = 0.5;
public static void main(String[] args) {
// 1-5 2-2.5 3-1.25 4-0.625 5-0.3125
for(int i=1; i<10; i++){
log.info("The current Height is : {} -- {}",i, getNewHeight(i, 10.0));
}
}
/**
* 在编写中出现的问题:
* (1)应该先判断是否为最后一次跳jumpCount == 0,开始的时候,我是先判断的jumpCount > 0
* (2) 没有给currentHeight变量赋值新值,一直使用的是初始值initialHeight;
* @param jumpCount 当前起跳的第几次;
* @param currentHeight 当前所在的高度;
* @return
*/
public static double getNewHeight(int jumpCount, double currentHeight) {
if (jumpCount == 0) {
return currentHeight;
}
currentHeight = currentHeight * JUMP_THRESHOLD;
-- jumpCount;
return getNewHeight(jumpCount, currentHeight);
}
}
截图:
考虑:
* 在编写中出现的问题: * (1)应该先判断是否为最后一次跳jumpCount == 0,开始的时候,我是先判断的jumpCount > 0 * (2) 没有给currentHeight变量赋值新值,一直使用的是初始值initialHeight;