Home > Enterprise >  Print several string patterns into one single line in console
Print several string patterns into one single line in console

Time:10-16

I am trying to print out numbers with # character drawing representation to the console. However, when I try to input two numbers, for example, they are printed out below each other not next to each other.

static void print(int mat[][])
{
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < 5; j  ) {
                if (i % 2 == 0) {
                    if (mat[i][j] == 1)
                        System.out.print("#");
                    else
                        System.out.print(" ");
                }
                else {
                    if (mat[i][j] == 1)
                        System.out.print("#");
                    else
                        System.out.print(" ");
                }
            }
            System.out.println();
        }

}

A matrix for the 5 number digit is defined as below:

    int mat[][] = { { 1, 1, 1, 1, 1 },
                    { 1, 0, 0, 0, 0 },
                    { 1, 1, 1, 1, 1 },
                    { 0, 0, 0, 0, 1 },
                    { 1, 1, 0, 1, 1 } };

The screenshot below is the output when I try to input 55 to my program (Expected output is to draw both digits next to each other with a space between them):

enter image description here

CodePudding user response:

public class Main {

    public static void main(String... args) {
        print("0123456789");
    }

    private static final String[][] DIGITS = {
            { "#####", "    #", "#####", "#####", "#   #", "#####", "#    ", "#####", "#####", "#####" },
            { "#   #", "  # #", "    #", "    #", "#   #", "#    ", "#    ", "    #", "#   #", "#   #" },
            { "#   #", "#   #", "#####", "#####", "#####", "#####", "#####", "    #", "#####", "#####" },
            { "#   #", "    #", "#    ", "    #", "    #", "    #", "#   #", "    #", "#   #", "    #" },
            { "#####", "    #", "#####", "#####", "    #", "#####", "#####", "    #", "#####", "    #" } };

    public static void print(String val) {
        for (int row = 0; row < DIGITS.length; row  ) {
            for (int i = 0; i < val.length(); i  ) {
                if (i > 0)
                    System.out.print(' ');

                int digit = Character.getNumericValue(val.charAt(i));
                System.out.print(DIGITS[row][digit]);
            }

            System.out.println();
        }

        System.out.println();
    }

}

Output:

#####     # ##### ##### #   # ##### #     ##### ##### #####
#   #   # #     #     # #   # #     #         # #   # #   #
#   # #   # ##### ##### ##### ##### #####     # ##### #####
#   #     # #         #     #     # #   #     # #   #     #
#####     # ##### #####     # ##### #####     # #####     #

CodePudding user response:

Here is the code that does what you want to do. But I think there should be another way around to obtain a more generic way.

    static void matPrint(int mat[][])
{
    int n = 10  ; // How many times you want to print side by side?
        for (int i = 0; i < 5; i  ) {
            for (int j = 0; j < (mat[0].length*n) (n-1); j  ) {
            if(j%6==5 && j!=0) {
                System.out.print(" ");
                continue ;
            }
                if (i % 2 == 0) {
                    if (mat[i][j%(mat[0].length 1)] == 1)
                        System.out.print("#");
                    else
                        System.out.print(" ");
                }
                else {
                    if (mat[i][j%(mat[0].length 1)] == 1)
                        System.out.print("#");
                    else
                        System.out.print(" ");
                }
            }
            System.out.println();
        }

}

CodePudding user response:

I'd suggest using 3d array.

int mat[][][] = 
                {
                    {   //insert 0th pattern and followed by 1 to 9.
                        { 1, 1, 1, 1, 1 },
                        { 1, 0, 0, 0, 0 },
                        { 1, 1, 1, 1, 1 },
                        { 0, 0, 0, 0, 1 },
                        { 1, 1, 0, 1, 1 } 
                    },
                    {   
                        { 1, 1, 1, 1, 1 },
                        { 1, 0, 0, 0, 0 },
                        { 1, 1, 1, 1, 1 },
                        { 0, 0, 0, 0, 1 },
                        { 1, 1, 0, 1, 1 } 
                    }
                };
int num = // user input;
print(mat,num);
static void print(int mat[][][],int num)
{
    int len = Integer.toString(num).length();
    int arr[] = new int[len];
    for(int i=len-1;i>=0;i--)
    {
        arr[i] = num%10;
        num = num/10;
    }
    // above are for splitting the num into array.
    // 5483 to {5,4,8,3}
    for (int i = 0; i < 5; i  ) {
        for(int k = 0;k < len;k  ){
            // this for-loop is for iterating one row of all the numbers
            // 1st row of 5, 4, 8, 3 and then 2nd row and so on.
            for (int j = 0; j < 5; j  ) {
                if (mat[arr[k]][i][j] == 1)
                    System.out.print("#");
                else
                    System.out.print(" ");
            }
            System.out.print("  ");
        }
        System.out.println();
    }
}
  •  Tags:  
  • java
  • Related