2016-04-12 51 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

很好的工作表明,没有新手写这篇文章 –