Java I';我试图将二维字符数组初始化为空格,但控制台中的输出很奇怪

Java I';我试图将二维字符数组初始化为空格,但控制台中的输出很奇怪,java,multidimensional-array,char,Java,Multidimensional Array,Char,下面是我的构造函数,我尝试用空格初始化数组,但是当我打印出*作为边框(在我的toString中)时,奇怪的是控制台中的输出 // 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

下面是我的构造函数,我尝试用
空格
初始化数组,但是当我打印出*作为边框(在我的toString中)时,奇怪的是控制台中的输出

// 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] = ' ';
        }
    }
}
//根据尺寸构造屏幕
公共屏幕(整数高度、整数宽度){
屏幕=新字符[高度+2][宽度+2];
对于(int i=0;i
产量低于


*
Ū
ʪ
Ϫ
Ԫ
٪
ު


୪୪୪୪୪୪୪୪୪୪୪

我有一个9乘9的屏幕,我不知道哪里出了问题

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;
}
公共字符串toString(){
字符串str=“”;
for(int row=0;row
您的toString看起来很奇怪

你写道:

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



更好的解决方案 前面的解决方案会起作用,Netheress,我建议您这样做:

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();
    }
}
公共静态类屏幕{
私有字符[][]屏幕;
私有静态最终字符串边框=“*”;
公共屏幕(整数高度、整数宽度){
屏幕=新字符[高度][宽度];
对于(int i=0;i

通过这种方式打印墙阵列,您不会覆盖边框内容。(在阵列周围添加星号,而不是在边框周围添加星号)

你的toString看起来很奇怪

你写道:

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



更好的解决方案 前面的解决方案会起作用,Netheress,我建议您这样做:

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();
    }
}
公共静态类屏幕{
私有字符[][]屏幕;
私有静态最终字符串边框=“*”;
公共屏幕(整数高度、整数宽度){
屏幕=新字符[高度][宽度];
对于(int i=0;i

通过这种方式打印墙阵列,您不会覆盖边框内容。(在阵列周围添加星号,而不是在边框周围添加星号)

如何打印阵列?@AnthonyRaymond请看我的编辑,任何建议都会有用什么是
边框
?为什么在你的toString()方法中它是递增的?你如何打印你的数组?@AnthonyRaymond请看我的编辑,任何建议都会有用什么是
边框
?为什么在你的toString()方法中它是递增的?非常感谢,这是我犯的一个愚蠢的错误。我添加了一个更高级的解决方案,看一看。非常感谢,这是我犯的一个愚蠢的错误。我添加了一个更高级的解决方案,看一看。