2012-09-06 55 views
1

我如何在我的編輯器中編寫simpla Java序列(使用notepad ++)?Java序列打印

我對基礎知識很熟悉,但無法在我的cmd中打印這一個。

int a = 1; a = a + a; a = a + a; a = a + a; ...

+2

你至今嘗試過什麼?告訴我們你做了什麼。 – kosa

+2

我會將它粘貼到IDE中一個'main(String [])'方法的主體中,添加System.out.println(a);然後按'[Run]'或者你可以直接輸出'8' –

+0

你是否想要打印'2 4 8 16 ....'序列 – John

回答

1

這應該是你在找什麼。

public static void main(String[] args) 
{ 
    int startValue = 1; 
    int numberOfAdditions = 10; 
    int currentValue = startValue; 
    for(int i = 0;i<numberOfAdditions;i++) 
    { 
     //do opperations here 
     currentValue = currentValue+currentValue; 
     //print out value 
     System.out.println(currentValue); 
    } 
} 
+0

如果你希望它們都在同一行上,你可以做System。 out.print(currentValue +「」); –

1

試試這個:

int a = 1; 
for (int i = 0 ; i < MAX_PRINTS ; i++) { 
    System.out.println(a); 
    a *= 2; 
} 

或者,如果你要打印,直到達到一定值:

int a = 1; 
while (a <= MAX_VALUE) { 
    System.out.println(a); 
    a *= 2; 
} 
+0

這是增加,而不是乘法。否則,這看起來很簡潔。 – Makoto

+0

對不起,我可能會錯過一些東西 - 你的意思是「它的增加」?在這個問題中,'a'的後續值是通過把'a'加到它自己,即乘以2來計算的。 – arshajii