需要有关简单Java模式编码的帮助

时间:2012-06-06 21:42:11

标签: java for-loop

我要在for循环中制作以下模式:

XXXXXXXXXX
XXXXXXXXXY
XXXXXXXXYY
XXXXXXXYYY
...

..等等

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

} 〜

我在输出结尾处得到额外的“X”,这是我不想要的。 我认为有更好的方法可以做到这一点,但现在想不出办法

任何帮助人员?

6 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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":"");
        }
}

teehee。

答案 2 :(得分:0)

您的具体问题的答案是:

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

i<=10应为i<10,因为从0到10(包括),有11个循环。

答案 3 :(得分: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);
}

答案 4 :(得分:0)

矩阵的/对角线的条件是i + j = n,因此左上部分是i + j&lt; n和i + j&gt; 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&gt; j和下部i&lt;学家

答案 5 :(得分: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;
      }
    }
  }