当前位置: 代码迷 >> J2SE >> 不用循环~怎么输出1~100
  详细解决方案

不用循环~怎么输出1~100

热度:443   发布时间:2016-04-24 18:02:12.0
不用循环~如何输出1~100?
RT~~~~~~~~~~~~~~~~~~~~~~~~
我想了好久
就想到应该用return
小弟刚刚学JAVA的~~

------解决方案--------------------
用递归!
------解决方案--------------------
递归
------解决方案--------------------
用递归! 

------解决方案--------------------
Java code
    public static void main(String args[])    {        printNum(1);    }        public static void printNum(int n)    {        if(n == 100)        {            System.out.println(n);        }        else        {            System.out.println(n);            printNum(n + 1);        }    }
------解决方案--------------------
Java code
public class Test1 {    public static synchronized void main(String[] a) {        int i =0;             getNumber(i);        }    static  void getNumber(int i)    {          i++;    System.out.println(i);        if (i < 100) getNumber(i);    }}
------解决方案--------------------
Java code
public class Test{   public void Test_1(int a)   {       if(a <= 100)       {       System.out.println(a);       Test(++a);       }                    }      public static void main(String[] args)   {       Test test = new Test();       test.Test_1(1);   }   }
------解决方案--------------------
code=Java]
public void printNum(int n)
{
if(n==1)
System.out.println(n);
else
{
printNum(n-1);
System.out.println(n);
}
}[
[/code]
------解决方案--------------------
用递归
Java code
printNum(100);public void printNum(int i){System.out.println(i);if(i==1){ return;}else{printNum(--i);}}
------解决方案--------------------
Java code
import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Test01 {    public static void main(String[] args) {        final int len = 100;        final Timer timer = new Timer();        timer.schedule(new TimerTask() {            private int i = 1;            public void run() {                System.out.println(i++);                if (i > len) {                    this.cancel();                    timer.cancel();                }            }        }, new Date(), 1);    }}
------解决方案--------------------
整理了一下2个方法
Java code
import java.util.Timer;import java.util.TimerTask;public class T {  public static void main(String[] arge) {    show(100);    show2(100);  }  public static void show(int num) {    if (num > 1) {      show(num - 1);    }    System.out.println(num);  }  public static void show2(final int num) {    final Timer timer = new Timer();    timer.schedule(new TimerTask() {      private int i = 0;      @Override      public void run() {        if (i <= num) {          System.out.println(i++);        } else {          timer.cancel();        }      }    }, 0, 1);  }}
------解决方案--------------------
探讨
Java code
public static void main(String args[])
{
printNum(1);
}

public static void printNum(int n)
{
if(n == 100)
{
System.out.println(n);
  相关解决方案