2

I am to make this following pattern on a for loop:

XXXXXXXXXX
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...

..and so on

public class ex{
    public static void main(String[] args){
            for(int i=0;i<=10;i++){
                    System.out.println();
                    for(int j=0;j<=10;j++){
                            if(i==0){
                                    System.out.print("X");
                            }

                            if(i==1){
                                    System.out.print("X");
                                    if(j==9){
                                            System.out.print("Y");
                                    }
                            }
                    }
            }
    }

} ~

I am getting extra "X" at the end for my output that I don't want. I think there is a better way to do this but can't think of a way right now

Any help guys?

4

6 回答 6

3

尝试在一个循环中嵌套两个循环。向上计数,然后在外循环的每次迭代中i继续计数:10

// 10 lines
for(int i = 10; i >= 0; i--){

    int j = 0; 

    // Print 'X's (10 - i of them)
    for(; j < i; j++)
        System.out.print("X");

    // Print 'Y's (i of them)
    for(; j < 10; j++)
        System.out.print("Y");

    System.out.println();
}
于 2012-06-06T21:46:27.603 回答
0

矩阵的 / 对角线的条件是 i + j = n,因此左上部分为 i + j < n,右下部分为 i + j > n。

public static void main(String... arg) {
    int n = 10;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ( i + j < n) {
                System.out.print("X");
            } else {
                System.out.print("Y");
            }
        }
        System.out.println();
    }
}

如果要在\对角线上分离,对角线的条件是i = j,上部分i > j,下部分i < j。

于 2012-06-06T21:56:44.947 回答
0

提示:每一行的长度相同,向下的每一行只有一个少一个 X 和一个多一个 Y。

public class XY
{
        public static void main(String[] args)
        {
                System.out.print(xy(10,10));
        }
        public static String xy(int rows,int origRows)
        {
                return X(rows,origRows-rows)+"\n"+((rows>0)?xy(rows-1,origRows):"");
        }
        public static String X(int x,int y)
        {
                return (x>0?"X":"")+((x>0||y>0)?X(x-1,y-1):"")+(y>0?"Y":"");
        }
}

嘻嘻。

于 2012-06-06T21:46:08.407 回答
0

您的具体问题的答案是:

for(int i=0;i<=10;i++)

i<=10应该是i<10,因为从 0 到 10(含)有 11 个循环。

于 2012-06-06T21:47:06.683 回答
0

可能不是最有效的版本,但在这里:

public static void main(String[] args) {
    for (int i = 0; i <= 10; i++) {
        System.out.println(repeat("X", 10 - i) + repeat("Y", i));
    }
}

private static String repeat(String string, int times) {
    return new String(new char[times]).replaceAll("\0", string);
}
于 2012-06-06T21:53:24.103 回答
0

这是一个递归解决方案:

 public class ex {
    public static final String X = "X";
    public static final String Y = "Y";

    public static void main(String[] args){
      printall(10, 0);
    }

    private static void printall(int length, int saturation){
      if (saturation > length) {
        return;
      } else {
        System.out.print(printRow(length, saturation, 0);
        printall(length, saturation + 1);
      }
    }

    private static String printrow(int length, int saturation, int position) {
      if (position > length) {
        return "";
      } else {
        return getChar(length, saturation, position) + printrow(length, saturation, position + 1);
      }
    }

    private static String getChar(int length, int saturation, int position) {
      if (length-saturation < position) {
        return Y;
      } else {
        return X;
      }
    }
  }
于 2012-06-06T22:02:17.353 回答