可能重複:
Display numbers from 1 to 100 without loops or conditions打印1至10沒有任何環在Java
面試問題:
打印1至10沒有任何的java循環。
可能重複:
Display numbers from 1 to 100 without loops or conditions打印1至10沒有任何環在Java
面試問題:
打印1至10沒有任何的java循環。
簡單的方法: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);
複雜的方式:使用遞歸
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.
你是如何在26秒的時間內做到這一點的? – Mysticial 2012-02-23 06:29:29
因爲Eclipse! :p我愛Eclipse。 – 2012-02-23 06:30:26
@HarryJoy:你的日食與光速似乎一樣...... D – 2012-02-23 06:31:26
如果你喜歡你的節目鈍,沒有循環,條件語句或主要方法。
static int i = 0;
static {
try {
recurse();
} catch (Throwable t) {
System.exit(0);
}
}
private static void recurse() {
System.out.print(++i + 0/(i - 11) + " ");
recurse();
}
它使用一個循環,但可能是一個有趣的答案
Random random = new Random(-6732303926L);
for(int i = 0; i < 10; i++)
System.out.print((1+random.nextInt(10))+" ");
}
可以重組這不是使用循環。
精彩的回答+1 – 2014-03-14 08:59:11
System.out.println(「1 to 10」); ...:D – 2012-02-23 06:29:41
*「面試問題:」*你的*答案是什麼?你是在你的手機上輸入這個問題,希望在面試官回來之前找到答案嗎? – 2012-02-23 07:16:20