【问题标题】:Simple System.out printing简单的 System.out 打印
【发布时间】:2016-08-03 09:46:23
【问题描述】:

好的,伙计们,我有这个问题,我该如何编码 但之间没有空行

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();
    }

}

【问题讨论】:

  • y 来自哪里?您究竟想消除哪些空行?
  • 如果你的意思是换行符,那是 println() 调用导致的。

标签: java arrays main public system.out


【解决方案1】:

模式如下:

1x, 0y

1x, 1y

2x, 1y

2x,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);

【讨论】: