简体   繁体   中英

I'm trying to initialize a 2d array of char into spaces, but the output in the console is weird

The following is my constructor, I try to initialize the array with spaces , but when I print out * as the border(in my toString), the weird thing is the output in the console

// constructs a screen based on the dimensions
public Screen(int height, int width) {
    screen = new char[height+2][width+2];
    for (int i = 0; i < screen.length; i++) {
        for (int j = 0; j < screen[i].length; j++) {
            screen[i][j] = ' ';
        }
    }
}

output is below


*
Ū
ʪ
Ϫ
Ԫ
٪
ު


୪୪୪୪୪୪୪୪୪୪୪

I constructor a 9 by 9 screen, I don't know where it went wrong.

public String toString() {
    String str = "";
    for (int row = 0; row < screen.length; row++) {
        for (int col = 0; col < screen[row].length; col++) {
            if (row == 0 || col == 0 || row == screen.length-1) {
                str += border;
            } else {
                border += screen[row][col];
            }
        }
        str += "\n";
    }
    return str;
}

Your toString looks weird.

you wrote :

if (row == 0 || col == 0 || row == screen.length-1) {
    str += border;
} else {
    border += screen[row][col];
}

it is supposed to be : str += screen[row][col];



A better solution

The previous solution will work, nethertheless, i'll recommend you to do it this way :

public static class Screen {
    private char[][] screen;
    private static final String BORDER = "*";

    public Screen(int height, int width) {
        screen = new char[height][width];
        for (int i = 0; i < screen.length; i++) {
            for (int j = 0; j < screen[i].length; j++) {
                screen[i][j] = ' ';
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        // add (screen.length + 2) for the first line.
        for (int i = 0; i < screen.length + 2; i++) {
            sb.append(BORDER);
        }
        sb.append(System.getProperty("line.separator"));

        for (int i = 0; i < screen.length; i++) {
            // star at the begin of each line
            sb.append(BORDER);
            for (int j = 0; j < screen[i].length; j++) {
                sb.append(screen[i][j]);
            }
            // star at the end of each line
            sb.append(BORDER);
            sb.append(System.getProperty("line.separator"));
        }

        // add (screen.length + 2) for the last line.
        for (int i = 0; i < screen.length + 2; i++) {
            sb.append(BORDER);
        }

        return sb.toString();
    }
}

This way the wall array is printed, you do not override border content. (stars are added around the array, not instead of the border )

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