标签:递归
public static void main(String[] args) {
System.out.println("打印小于50的数,逆序");
printOne(2);
}
public static void printOne(int n) {
//System.out.println(n);
if (n < 50)
printOne(2 * n);
else {
//System.out.println(n);
printTwo(n / 2);
}
}
public static void printTwo(int n) {
if (n >= 2) {
System.out.println(n);
printTwo(n / 2);
}
}说明:一个整数,大于0,不用循环和本地变量,按照n,2n,4n,8n的顺序递增,当值大于50时,把值按照指定顺序输出来。 假设n = 2;
参考:http://blog.csdn.net/xiaorenhui1030/article/details/37940621
/**
* 递归小例子:比如打印小于m的数,
* 每次打印后加1
* *******************
* @param i
* @param m
* *******************
* @author:wind
* 2014-8-10 下午7:07:25
* *******************
*/
public static void testOne(int i,int m) {
if(i < m) {
System.out.println(i);
//i++;
i = i * 2;
testOne(i, m);
}
}本文出自 “我的JAVA世界” 博客,请务必保留此出处http://hanchaohan.blog.51cto.com/2996417/1538260
标签:递归
原文地址:http://hanchaohan.blog.51cto.com/2996417/1538260