早教吧 育儿知识 作业答案 考试题库 百科 知识分享

1、假设一条绳子长3000米,每天减去一半,请问需要花费几天的时间,使绳子的长度会短于五米.用JAVA程序

题目详情
1、假设一条绳子长3000米,每天减去一半,请问需要花费几天的时间,使绳子的长度会短于五米.用JAVA程序
▼优质解答
答案和解析
这个问题不难,给你写了一小段代码,改下包名,运行下
package 递归;
public class Line {
/**
* 天数
*/
private static int day = 0;
private static int limit = 5;
public static void main(String[] args) {
int length = 5000;
discount(length);
}
private static void discount(int length){
length = length/2;
day++;
System.out.println("今天是第"+day+"天,绳子今天的长度为:"+length);
if(length > limit){
discount(length);
}
}
}