2012-02-23 60 views

回答

20

簡單的方法: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. 
+9

你是如何在26秒的時間內做到這一點的? – Mysticial 2012-02-23 06:29:29

+10

因爲Eclipse! :p我愛Eclipse。 – 2012-02-23 06:30:26

+1

@HarryJoy:你的日食與光速似乎一樣...... D – 2012-02-23 06:31:26

4

如果你喜歡你的節目鈍,沒有循環,條件語句或主要方法。

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))+" "); 
} 

可以重組這不是使用循環。

+0

精彩的回答+1 – 2014-03-14 08:59:11

相關問題