Home > OS >  Trying to print a tile in console in java
Trying to print a tile in console in java

Time:11-20

I'm trying to make a tile pattern generator method. I'm not sure how to do for loops so that once the x char meets at the center, then they go to the opposite diagonal.

My code so far is:

public static void crosses(int x, int y, char g, char filler,boolean p) {

    if(p== false) {
        int temp = 0;
        for(int i=0;i<y;i  ) {
            for(int j=0;j<x;j  ) {                  
                if(j == 2 i||j==8-i) 
                    output[i][j] = "x";                     
                else
                    output[i][j] = ".";
            }                   
        }   
                
        for(int i=0;i<length;i  ) {
            for(int j=0;j<height;j  ) {
                System.out.print(output[i][j]);
            }
            System.out.println();
        }               
    }
}

g.crosses(12, 12, 'x', '.', false); should output

..x......x..
...x....x...
x...x..x...x
.x...xx...x.
..x..xx..x..
...xx..xx...
...xx..xx...
..x..xx..x..
.x...xx...x.
x...x..x...x
...x....x...
..x......x..

I tried using a temp variable to increment but had no luck with the issue stated above

CodePudding user response:

public static void crosses(int height, int length, char main, char filler, boolean pattern) {
    char[][] output = new char[height][length];
    if(pattern == false) {
        for(int i = 0; i<length; i  ) {
            for (int j = 0; j < height; j  ) {
                if (j == (i - 2) || j == (i   2) || j == (height - i - 3) || j == (height - i   1)) {
                    output[i][j] = main;
                }
                else {
                    output[i][j] = filler;
                }
            }
        }
    }
    for (char[] row : output) {
        for (char c : row) {
            System.out.print(c);
        }
        System.out.println();
    }
}

The above works as close to your code as possible, but here are a few improvements to the way you wrote it:

public static char[][] crosses(int height, int length, char main, char filler, boolean pattern) {
    char[][] output = new char[height][length];
    if (pattern) {
        for (char[] row : output) { Arrays.fill(row, filler); }
        return output; // I made an assumption that if true, the output should be filled with filler only.
    }
    // no need to do pattern == false, just leave using return if true.
    for(int i = 0; i<length; i  ) {
        for (int j = 0; j < height; j  ) {
            if (j == (i - 2) || j == (i   2) || j == (height - i - 3) || j == (height - i   1)) {
                output[i][j] = main;
            }
            else {
                output[i][j] = filler;
            }
        }
    }
    return output; // Better to delegate the print to the caller, so they can decide how to print it.
}

public static void main(String[] args) {
    char[][] result = crosses(12, 12, 'x', '.', false);
    for (char[] row : result) {
        System.out.println(row); // better than another for loop as it's already built in.
    }
}
  • Related