在java中没有任何循环打印1到10

Har*_*Joy 5 java

可能重复:
显示从1到100的数字,没有循环或条件

面试问题:

在java中没有任何循环打印1到10.

Har*_*Joy 21

简单方法:System.out.println值:

    System.out.println(1);
    System.out.println(2);
    System.out.println(3);
    System.out.println(4);
    System.out.println(5);
    System.out.println(6);
    System.out.println(7);
    System.out.println(8);
    System.out.println(9);
    System.out.println(10);
Run Code Online (Sandbox Code Playgroud)

复杂的方式:使用递归

public void recursiveMe(int n) {
    if(n <= 10) {// 10 is the max limit
        System.out.println(n);//print n
        recursiveMe(n+1);//call recursiveMe with n=n+1
    }
}
recursiveMe(1); // call the function with 1.
Run Code Online (Sandbox Code Playgroud)

  • 因为Eclipse !!! :p我喜欢Eclipse. (10认同)
  • 你是如何在26秒内做到这一点的? (9认同)
  • 只需编写syso然后ctrl + space.然后alt +向下复制.然后在某处键入recursiveMe并按ctrl + space.然后写,如果条件和syso.它非常简单快捷. (5认同)
  • 你是小乔恩斯基特.;) (4认同)
  • 可能我很快但不像*Jon Skeet*. (2认同)