有人能解释一下Java嵌套循环是如何使用数字模式的吗



我很难理解嵌套循环和数字模式是如何工作的。我已经完成了除3以外的所有图案。有人能帮我处理这个代码并解释它是如何工作的吗?

public class Patterns7{
    public Patterns7() {
    }
    public void displayPatternI(int lines) 
    {
        System.out.println("ntPattern In");
        for (int i = 1; i <= lines; i++)
        {
            for (int j = 1; j <= i; j++)
                System.out.print (j + " " );
            System.out.println();
        }
    }
    public void displayPatternII (int lines) 
    {
        System.out.println("ntPattern II to be implementedn");
        for (int i = 1;i <= lines; i++) 
        { 
            for(int j = i;j >= 1; j--) 
                System.out.print(j); 
            System.out.println(); 
        }
        System.out.println();
    }
    public void displayPatternIII (int lines) 
    {
        System.out.println("ntPattern III  to be implementedn");
        for (int i = 1; i <= lines; i++)
        {
            for (int space = 1; space <= lines-i; space++)
                System.out.print (" ");
            for (int j = 1; j <= i; j++)
                System.out.print (j + " ");
            System.out.println();
        }
        System.out.println();
    }
}

模式III应该是这样的:

      6
     56
    456
   3456
  23456
 123456

但我所能做的就是:

      1
     1 2
    1 2 3
   1 2 3 4
  1 2 3 4 5
 1 2 3 4 5 6

我不确定如何从6开始,然后减少再增加。

模式V应该是这样的:

          1
        2 1 2
       3 2 1 2 3
      4 3 2 1 2 3 4
     5 4 3 2 1 2 3 4 5
    6 5 4 3 2 1 2 3 4 5 6

但结果是这样的:

    1
     1 2 3
    1 2 3 4 5

代码:

public void displayPatternVI (int lines) 
{
    System.out.println("ntMy Own Pattern to be implementedn");
    for (int i = 1; i <= lines/2; i++)
    {
        for (int space = 1; space <= (lines/2)-i; space++)
            System.out.print (" ");
        for (int j = 1; j <= (i*2)-1; j++)
            System.out.print (j + " ");
        System.out.println();
    }
    for (int i = 1; i <= lines/2; i++)
    {
        for (int space = 1; space <= i-1; space++)
            System.out.print (" ");
        for (int j = 1; j <= lines-(i*2)+1; j++)
            System.out.print (j + " ");
        System.out.println();
    }
    System.out.println();
}

模式VI应该是这样的:

              1
            2 1 2
          3 2 1 2 3
        4 3 2 1 2 3 4
      5 4 3 2 1 2 3 4 5
    6 5 4 3 2 1 2 3 4 5 6
    6 5 4 3 2 1 2 3 4 5 6
      5 4 3 2 1 2 3 4 5 
        4 3 2 1 2 3 4
          3 2 1 2 3
            2 1 2
              1

但看起来是这样的:

      1
     1 2 3
    1 2 3 4 5
    1 2 3 4 5
     1 2 3
      1

有人能帮我解释一下怎么做吗?

实际上一次只应该问一个问题,但这里有一个解决第一个问题的方法。对于SIZE等于6:

    6
   56
  456
 3456
23456

123456

你会想要这样的东西:

String temp;
for(int i = 0; i < SIZE; i++)
{
    temp = "";
    for(int j = SIZE - i; j <= SIZE ; j++) 
    {
        temp += j;
    }
System.out.printf("%" + SIZE + "sn", temp);
}

你自己试试第二个。

public void displayPatternIII (int lines) 
{
  System.out.println("ntPattern III  to be implementedn");
    for (int i = lines; i >= 1; i--)
      {
        int space = 1;
        for (; space <= i-1; space++)
          System.out.print (" ");
        for (int j = space; j <= lines; j++)
          System.out.print (j);
        System.out.println();
      }
}

输出:

    Pattern III  to be implemented
     6
    56
   456
  3456
 23456
123456

最新更新