Display the pyramid of the number plate

I did it below, but I did not understand

int i = 1;
       while(i <= 6){
             for(int j = 1;j <= 6-i;j++){
                   System.out.print("  ");
            }
            for(int m = 1; m <= i; m++){
                   System.out.print(m + " ");
            }
            i++;
            System.out.println();
     }

I got this instead:

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

But I need guidance on how to do this below,

          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  
+4
source share
4 answers

There you have my solution, a small solution for smartphones using absolute values, with notes why exactly there

public static void printPyramid(int rows) {
        // row counter
        for (int i = 1; i <= rows; i++) {

            // padding- size = rows - i
            for (int j = 1; j <= rows - i; j++) {
                // 2 spaces - char + space
                System.out.print("  ");
            }

            // print numbers
            for (int j = -i; j <= i; j++) {
                // we want only once 1, and skip print zero
                if (j == 0 || j == 1) {
                    continue;
                }

                // print absolute value
                System.out.print(Math.abs(j) + " ");
            }

            // new row- println same as print("\n");
            System.out.println();
        }
    }

6-line output

          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
source
while(i <= 6){
    for(int j = 1; j <= 6 - i; j++){
        System.out.print("  ");
    }
    for(int m = i-1; m > 1; m--){
        System.out.print(m + " ");
    }
    for(int m = 1; m < i; m++){
        System.out.print(m + " ");
    }

    i++;
    System.out.println();
}

This should work for you, I just added / edited this part:

    for(int m = i-1; m > 1; m--){
        System.out.print(m + " ");
    }
    for(int m = 1; m < i; m++){
        System.out.print(m + " ");
    }

To count the counter again, and let it count later

+4
source

, , :

public class Main {

    public static void main(String[] args) {

        for (int i = 0; i <= 6; i++) {
            printRow(6, i);
        }
    }

    public static void printRow(int highestValue, int rowValue) {

        for (int i = 0; i < (highestValue - rowValue); i++) {
            System.out.print("  ");
        }

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

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

        System.out.println();
    }
}

, . 1, . 2 . , 2 , i = 2 , rowValue, 1, .

:

          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, , . , ( ), , . printRow, , , .

+3

. , . . "1", "" ( ).

private static String buildLevel(int num) {
    if (num == 1) return "1";

    return Integer.toString(num) + " " + buildLevel(num -1) + " " + Integer.toString(num);
}

, , .

private static String pad(String stringToPad, int padTo) {
    return String.join("", Collections.nCopies(padTo - stringToPad.length(), " ")) + stringToPad;
}

, , .

private static String buildPyramid(int height) {
    int expectedLength = height * 2 + 1;

    String out = "";

    for (int i = 1; i <= height; i++) {
        out += pad(buildLevel(i), expectedLength) + "\n";
        expectedLength += 2;
    }

    return out;
}

- height * 2 + 1, . ( , ). 2 , .

,

public static void main(String[] args) {
    System.out.println(buildPyramid(5));
}

:

          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

For completeness, here is the whole code in one block.

private static String buildLevel(int num) {
    if (num == 1) return "1";

    return Integer.toString(num) + " " + buildLevel(num -1) + " " + Integer.toString(num);
}

private static String pad(String stringToPad, int padTo) {
    return String.join("", Collections.nCopies(padTo - stringToPad.length(), " ")) + stringToPad;
}

private static String buildPyramid(int height) {
    int expectedLength = height * 2 + 1;

    String out = "";

    for (int i = 1; i <= height; i++) {
        out += pad(buildLevel(i), expectedLength) + "\n";
        expectedLength += 2;
    }

    return out;
}

public static void main(String[] args) {
    System.out.println(buildPyramid(6));
}
0
source

Source: https://habr.com/ru/post/1695762/


All Articles