2016-04-12 52 views
0

玉傢伙我有這個問題我怎麼簡單的System.out打印

X

XY

XXY

XXYY

XXXYY之間的代碼本 但沒有空行

xxxyyy

這裏是我到目前爲止的代碼

public static void main(String[] args) { 

    System.out.print("x"); 
    for(int i = 0;i<6;i++){ 

     for(int j = 0;j<i;j++){ 
      System.out.print("x"); 
     } 
     System.out.println(); 
    } 

} 
+0

'y'從哪裏來?你想要消除哪些空行? –

+0

如果您的意思是換行符,則是導致該錯誤的'println()'調用。 –

回答

2

的模式如下:

1X,0Y

1X,1Y

2倍,1Y

2倍, 2y ...

所以你的循環應該看起來像這樣:

int xCount = 0; 
int yCount = 0; 
int total = 3; 
do { 
    if (xCount == yCount) xCount++; 
    else yCount++; 
    for (int x = 0; x < xCount; x++) System.out.print("x"); 
    for (int y = 0; y < yCount; y++) System.out.print("y"); 
    System.out.println(); 
} while (yCount < total); 
+1

非常感謝你 – Jorgovanka

+0

很好的工作表明,沒有新手寫這篇文章 –