对于我一生,我无法弄清楚为什么我的代码只是简单地以直线而不是5x5的比例打印出来。我知道这是一个简单的解决方案,但几天来我一直在为不同的程序编写代码,但现在还无法解决所有问题,感谢您的帮助!

导入java.util.Random;
公共类RandomWordGame {

private static char[][] letterBoard = new char[5][5];
private static Random r = new Random();

private static char[][] createBoard()
{
    for (int i=0; i<letterBoard.length; i++)
    {
        for (int j=0; j<letterBoard[i].length; j++)
        {
            letterBoard[i][j] = (char) (r.nextInt(26) + 'a');
            System.out.print(letterBoard[i][j]);
        }
    }
    return letterBoard;
}

public static void main(String[] args)
{
    letterBoard = createBoard();
}


}

最佳答案

只需在内循环之后添加换行符

for (int i=0; i<letterBoard.length; i++) {
    for (int j=0; j<letterBoard[i].length; j++) {
        letterBoard[i][j] = (char) (r.nextInt(26) + 'a');
        System.out.print(letterBoard[i][j]);
    }
    System.out.println();
}
return letterBoard;

09-25 22:18