Java打印带有嵌套for循环的模式

Java打印带有嵌套for循环的模式,java,for-loop,nested,Java,For Loop,Nested,我正在努力构建一种算法,以打印出急需的图案。 代码如下: public static void printPatternH(int size) { for (int row = 1; row <= size; row++) { for (int col = 1; col <= 2*size; col++) { if (col > size + row - 1) { conti

我正在努力构建一种算法,以打印出急需的图案。 代码如下:

public static void printPatternH(int size)
{
    for (int row = 1; row <= size; row++)
    {
        for (int col = 1; col <= 2*size; col++)
        {
            if (col > size + row - 1) {
                continue;
            }
            if (col <= size) {
                System.out.print((row + col >= size + 1 ? (row + col)%size : " ") + " ");
            }
            else {
                System.out.print((row + col >= size + 1 ? (row + size)%col : " ") + " ");
            }                
        }
        System.out.println();
    }
}
publicstaticvoidprintpatternh(int-size)
{
对于(int row=1;row=size+1)(row+size)%col:)+;
}                
}
System.out.println();
}
}
结果是:

<>我知道,如果<代码> size <代码> 9,中间的最后一个数将是0,如<代码>(行+大小)%CL=0 < /COD>,但是我不知道如何修改它,而不必改变其余的值。

< p>您可以检查“0”并在打印出来之前将其替换。

 if (col <= size) {
     //print left hand side
     int remainder =  (row + col) % size;
     if (remainder == 0) remainder = size; //replace the "0" with size here.
     System.out.print((row + col >= size + 1 ? remainder : " ") + " ");
} else {
     //print right hand side
     System.out.print((row + col >= size + 1 ? (row + size) % col : " ") + " ");
}

这是你问题的答案 我希望这对你有帮助

 int rowCount = 1;

    System.out.println("Here Is Your Pyramid");

    //Implementing the logic

    for (int i = noOfRows; i > 0; i--)
    {
        //Printing i*2 spaces at the beginning of each row

        for (int j = 1; j <= i*2; j++)
        {
            System.out.print(" ");
        }

        //Printing j value where j value will be from 1 to rowCount

        for (int j = 1; j <= rowCount; j++)             
        {
            System.out.print(j+" ");
        }

        //Printing j value where j value will be from rowCount-1 to 1

        for (int j = rowCount-1; j >= 1; j--)
        {                 
            System.out.print(j+" ");             
        }                          

        System.out.println();

        //Incrementing the rowCount

        rowCount++;
    }
int rowCount=1;
System.out.println(“这是你的金字塔”);
//实现逻辑
对于(int i=noOfRows;i>0;i--)
{
//在每行开头打印i*2个空格
对于(intj=1;j变化

试试这个

public class Testb 
{
    int item = 9;
    int limit = 5;
    int half = item/2;
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        for(int i=0;i<limit;i++){

           for(int j=0;j<half;j++){

              if(j<(half-i)){

                 print(" ");

              }else{

                 print(j-(half-i))

              }          
           }

           print(i);

           for(int k=half;k<(half+i);k++){

               print((half+i)-(k+1));

           }

           println();
        }
    }
}

希望它能帮助你

所以你想打印“9”而不是“0”?
(行+列)%size
->
(行+列-1)%size+1
@saka1029,这就是答案。是否发布它:)@AgelKoh是的,没错,saka1029的解决方案满足了我的疑问:)谢谢,她没有问一个完全不同于他的方法,但要解决他的方法,他不会学的,他只是用另一种方法。
(row + col)%size
(row + col - 1) % size + 1
public class Testb 
{
    int item = 9;
    int limit = 5;
    int half = item/2;
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        for(int i=0;i<limit;i++){

           for(int j=0;j<half;j++){

              if(j<(half-i)){

                 print(" ");

              }else{

                 print(j-(half-i))

              }          
           }

           print(i);

           for(int k=half;k<(half+i);k++){

               print((half+i)-(k+1));

           }

           println();
        }
    }
}
          0
        0 1 0
      0 1 2 1 0
    0 1 2 3 2 1 0
  0 1 2 3 4 3 2 1 0