简体   繁体   中英

Creating this number pattern in Java

I am trying to create a pattern in java by using a for-loop and nested for-loop based on the user input of a positive integer.

Here is an example:

User input = 5

Output:

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

This is what I have so far. I am getting a list of integers, but I cannot find out where to go from here to get a pattern such as the one above. All I am getting is "5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 "

System.out.print("Enter the value of n: ");
int n = scan.nextInt();
for (int row = 1; row <= n; row++) { //rows
    for (int col = 1; col <= n; col++) { //columns
        if (n - col + 1 <= n - row + 1 && row <= n && col <= n) { //what to print
            System.out.print(n - row + 1 + " ");
        } else if (n - col + 1 <= n - row + 1 && row <= n && col <= n) {
            System.out.print(n - row + 1 + " ");
        }
    }
}

In this code I'm mirroring the column's and after that mirroring the rows. From mirroring I mean first creating mirror of the data.

First writing data to columns(left side) and then mirroring the column hence I get the right side of the column.

After writing data to the upper rows then mirroring the upper row I get bottom row.

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class Pattern {
    static final PrintStream out = System.out;

    public static void displayPattern(int n) {
        final int arr[][] = new int[n * 2 - 1][];

        for (int i = 0; i < arr.length; i++)
            arr[i] = new int[n * 2 - 1];

        final List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int current = n - i;
            list.add(current);
            for (int j = 0; j < list.size(); j++) {
                arr[i][j] = list.get(j);
            }
            int remainings = n - list.size();
            for (int j = list.size(), count = 0; count < remainings; j++, count++) {
                arr[i][j] = current;
            }

            //writing data to right side of the columns(mirroring columns)
            for (int j = n, count = 1; j < arr[i].length; j++, count++)
                arr[i][j] = arr[i][j - count * 2];
        }
        //writing data to bottom half of the rows(mirroring rows)
        for (int i = n, count = 1; i < n * 2 - 1; i++, count++) {
            for (int j = 0; j < n * 2 - 1; j++)
                arr[i][j] = arr[i - count * 2][j];
        }


        for (int a[] : arr) {
            for (int b : a)
                out.print(b + " ");
            out.println();
        }

    }

    public static void main(final String... $) {
        displayPattern(5);
    }
}

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM