How to print a pattern number

So, I got this task: (Display Models) Write a way to display the template as follows:

enter image description here

Method Title: public static void displayPattern (int n)


Basically, I understood “HOW” to do the executive, and even coded it myself and got 99% of the code. I know that I need to do 2 cycles, one of which prints spaces and each time decreases by 1, and the other, which prints a number after a space and tilts by 1.

Here is my method:

public static void printPattern(int n) {
    int m =1;
    int k=1;
    while (m-1-1 <=n) {
        int numberOfWhiteSpaces = n -1;
        for (int i = numberOfWhiteSpaces; i >= 0; i--) {
            System.out.print(" ");
        }
        for (k=m; k>0; k--) {
            System.out.print( k + "");
        }
        System.out.println();
        m++;
        n--;
    }
}

Let's say i call

printPattern(3);

My only problem is that the output is as follows:

   1
  21
 321

There are no spaces between the numbers and yes, I tried to change this:

System.out.print( k + "");

:

System.out.print( k + " ");

Result?

enter image description here

I dealt with this problem for 2 hours in a row, I could not get it right. Maybe it will help you, thanks guys.

+4
1

, : , , . k + " " , , ( , ). , k + " " , System.out.print(" "); ( ) .

. : , , .

+7

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


All Articles