Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组混乱 char[]boardGame=newchar[10][10]; //假设这是为了创建一个数组对象 用于(字符[]行:棋盘游戏) { 数组。填充(行“a”); } //假设用字符“*”填充数组中的行 //我使用“增强型”循环 对于(int i=0;i_Java_Arrays_Multidimensional Array - Fatal编程技术网

Java 数组混乱 char[]boardGame=newchar[10][10]; //假设这是为了创建一个数组对象 用于(字符[]行:棋盘游戏) { 数组。填充(行“a”); } //假设用字符“*”填充数组中的行 //我使用“增强型”循环 对于(int i=0;i

Java 数组混乱 char[]boardGame=newchar[10][10]; //假设这是为了创建一个数组对象 用于(字符[]行:棋盘游戏) { 数组。填充(行“a”); } //假设用字符“*”填充数组中的行 //我使用“增强型”循环 对于(int i=0;i,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,在上面的内部for循环中,“i”和“j”已被数字覆盖,因为我声明它int i=0,如果条件为truei++,我会更新它 为什么它会打印********************************************而不是**********//code>,返回存储在维度中索引j中的字符,并且因为您已经用星号填充了整个数组,所以这就是打印的内容。如果要打印数字,请将boardGame[i][j]替换为j 我建议您阅读有关阵列工作原理的详细文档 您的数组中完全填充了字符'a',您怎么能期望您

在上面的内部
for
循环中,“i”和“j”已被数字覆盖,因为我声明它
int i=0
,如果条件为
true
i++
,我会更新它


为什么它会打印
********************************************
而不是
**********//code>,

返回存储在维度
中索引
j
中的
字符,并且因为您已经用星号填充了整个数组,所以这就是打印的内容。如果要打印数字,请将
boardGame[i][j]
替换为
j


我建议您阅读有关阵列工作原理的详细文档

您的数组中完全填充了字符
'a'
,您怎么能期望您的代码输出
|1 | | 2 | | 3 |……

如果要打印这样的内容,可以按如下方式初始化数组:

char[][] boardGame = new char[10][10];
// this is suppose to create an array object

for (char[] row : boardGame)
{
    Arrays.fill(row, 'a');
}
// this is suppose to fill the row in array with the char '*'
// and im using "Enhanced" for loops

for (int i = 0; i < boardGame.length; i++)
{

    for (int j = 0; j < boardGame[i].length; j++)
    {
        System.out.print("|" + boardGame[i][j] + "|");
    }
    // in the for loop above isn't "i" and "j" been overwrite with
    // numbers as I declare it int i = 0 and I update it if the
    // condition is true i++

    // how come it print|*||*||*||*||*|....instead of |1||2||3|...?

    System.out.println();
}
char[]boardGame=newchar[10][10];

对于(int i=0;i)你想让我们说什么?你的问题是什么?!我不明白为什么你希望它打印数字而不是星号。
char[][] boardGame = new char[10][10];
for (int i=0 ; i<boardGame.length ; i++)
    for (int j=0 ; j<boardGame[0].length ; j++) 
        boardGame[i][j] = Character.forDigit(j,10);