简体   繁体   中英

java array to print square box of asterisk

I'm trying to write a square shape of asterisk around border using the https://stackoverflow.com/a/34209565 answer.

But I cannot get it working at all.

Here is the code I'm trying.

    int _i = 10;
    int _j = 10;
    String[][] array = new String[_i][_j];
    for (int i = 0; i < _i; i++) {
        System.out.println();
        for (int j = 0; j < _j; j++) {
            if(i==0 || j == 0 || i == _i - 1|| j == _j - 1){
                array[i][j] = "*";
                System.out.print(array[i][j]);

            }
        }
    }

The output I'm getting is:

**********
**
**
**
**
**
**
**
**
**********

I tried running tthe code from the answer but it produces a one line of asterisk. Something from the code in the answer has been omitted.

The code provdied in the answer is:

int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
    for (int j = 0; j < _j; j++) {
        if(i==0 || j == 0 || i == _i-1|| j == _j-1){
            array[i][j] = "*";
        }
    }
}

And the output in the answer is:

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

You need an else to print a space when the entry is not a star. Also, I would move the println() to the end of the loop (instead of the beginning). Assuming you actually want to fill the array too, populate it with a space as well. Like,

for (int j = 0; j < _j; j++) {
    if (i == 0 || j == 0 || i == _i - 1 || j == _j - 1) {
        array[i][j] = "*";
    } else {
        array[i][j] = " ";
    }
    System.out.print(array[i][j]);
}
System.out.println();

Outputs

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

You can use the repeat method, so the code is less verbose, also enclosing the generation in a method you can make it dynamic bu setting two arguments: number of lines and number of columns:

class Test
{
    public static void main(String[] args)
    {
        System.out.println(createBox(10, 10));
        System.out.println(createBox(10, 5));
    }

    public static String createBox(int qtaRig, int qtaCol)
    {
        String result = "";
        for(int r = 1; r <= qtaRig; r++)
        {
            if(r == 1 || r == qtaRig)
            {
                result += "*".repeat(qtaCol);
            }
            else
            {
                result += "*" + " ".repeat(qtaCol-2) + "*";
            }
            result += "\n";
        }

        return result;
    }
}

Result:

**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********

*****
*   *
*   *
*   *
*   *
*   *
*   *
*   *
*   *
*****

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