初始化2d char数组以测试打印的错误

时间:2014-10-22 14:47:00

标签: java arrays printing char multidimensional-array

我的飞机座位表打印不正确。只有在输入预先初始化的数组时,我才会得到一长串错误。我不确定我目前做错了什么,但它不起作用。

import java.util.*;
public class AirplaneSeating
{
    public static void main(String[] args) throws IOException
    {
        Scanner console = new Scanner(System.in);
        int rows = 2;
        int c, c2;
        char[][] seatsLeft = new char[rows][3];
        char[][] seatsRight = new char[rows][3];
        seatsLeft = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
        seatsRight = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
        System.out.println("        A B C  D E F");
        for (c = 0; c < 6; c++)
        {
            System.out.print("Row  " + (c + 1) + " ");
            for (c2 = 0; c2 < 3; c2++)
            {
                System.out.print(seatsLeft[c2] + " ");
            }
            System.out.print("  ");
            for (c2 = 0; c2 < 3; c2++)
            {
                System.out.print(seatsRight[c2] + " ");
            }
            System.out.println();
        }
    }
}

以下是我遇到的错误:

AirplaneSeating.java:11: error: illegal start of expression
    seatsLeft = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
                ^
                                   ^
AirplaneSeating.java:12: error: <identifier> expected
    seatsRight = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
              ^

编辑:这是我到目前为止修复的代码,我没有更多的错误。

import java.util.*;
public class AirplaneSeating
{
    public static void main(String[] args) 
    {
        Scanner console = new Scanner(System.in);
        int rows = 3;
        int c, c2;
        char[][] seatsLeft = {{ '-', '-', '-' },{ '-', '-', '-' },
        { '-', '-', '-' }};
        char[][] seatsRight = {{ '-', '-', '-' },{ '-', '-', '-' },
        { '-', '-', '-' }};
        System.out.println("        A B C  D E F");
        for (c = 0; c < rows; c++)
        {
            System.out.print("Row  " + (c + 1) + " ");
            for (c2 = 0; c2 < 3; c2++)
            {
                System.out.print(seatsLeft[c2] + " ");
            }
            System.out.print("  ");
            for (c2 = 0; c2 < 3; c2++)
            {
                System.out.print(seatsRight[c2] + " ");
            }
            System.out.println();
        }
    }
}

但它打印出来像这样:        A B C D E F. 第1行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302 [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69 第2行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302 [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69 第3行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302 [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69

3 个答案:

答案 0 :(得分:1)

你需要这样做

char[][] seatsLeft = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
char[][] seatsRight = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};

或者像

一样循环
char[][] seatsLeft = new char[rows][3];
char[][] seatsRight = new char[rows][3];
for(int i=0i<rows;i++){
    for(int j=0;j<3;j++){
        seatsLeft[i][j]='-';
        seatsRight[i][j]='-';
    }
}

你不能这样做

char[][] seatsLeft = new char[rows][3];
char[][] seatsRight = new char[rows][3];
seatsLeft = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
seatsRight = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};

因为您在执行char[][] seatsLeft = new char[rows][3];时已经初始化了数组!

当您看到我们如何使用2循环初始化时,您还必须使用2个循环进行打印。

for(int i=0;i<rows;i++){
    System.out.println("Row " + (i + 1));
    for(int j=0;j<3;j++){
        System.out.print(" "+seatsLeft[i][j]);
    }
    System.out.print(" ");
    for(int j=0;j<3;j++){
        System.out.print(" "+seatsRight[i][j]);
    }
    System.out.println("");
}

**编辑:**

它打印出来像这样:ABCDEF第1行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302 [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69第2行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302] [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69第3行[C @ 55f96302 [C @ 55f96302 [C @ 55f96302 [C @ 3d4eac69 [C @ 3d4eac69 [C @ 3d4eac69

对你而言,因为你必须明白你在做什么

 System.out.print(seatsLeft[c2] + " ");
 System.out.print(seatsRight[c2] + " ");

您需要了解您所做的是在二维阵列上。 seatLeft [c2]&amp; seatsRight [c2]返回您所看到的那一行的地址。

答案 1 :(得分:1)

此计划中有两个主要问题 - 1.您不需要抛出IOException,或者为了进一步的目的而不需要导入java.io.IOException;

  1. 在定义点初始化你的char [] [],即
  2. &#13;
    &#13;
    char[][] seatsLeft = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
    char[][] seatsRight = {{'-','-','-'},{'-','-','-'},{'-','-','-'}};
    &#13;
    &#13;
    &#13;

    如果你想稍后再做,请逐个元素,即char [0] [0] =&#39; - &#39;;

    如果您进行此修改,您的程序将会运行。

答案 2 :(得分:0)

我在你的代码中看到了一些错误,首先你的数组赋值(使用括号)只允许在数组的初始声明中。使用数组.length属性而不是硬编码长度。然后,要访问和打印二维数组中的字符,请使用两个数组索引。像,

char[][] seatsLeft = { { '-', '-', '-' }, { '-', '-', '-' },
        { '-', '-', '-' } };
char[][] seatsRight = { { '-', '-', '-' }, { '-', '-', '-' },
        { '-', '-', '-' } };
System.out.println("        A B C  D E F");
for (c = 0; c < seatsLeft.length; c++) {
    System.out.print("Row  " + (c + 1) + " ");
    for (c2 = 0; c2 < seatsLeft[c].length; c2++) {
        System.out.print(seatsLeft[c][c2] + " "); // <-- [c][c2]
    }
    System.out.print("  ");
    for (c2 = 0; c2 < seatsRight.length; c2++) {
        System.out.print(seatsRight[c][c2] + " "); // <-- [c][c2]
    }
    System.out.println();
}

或者,使用for-each和格式化输出

for (c = 0; c < seatsLeft.length; c++) {
    System.out.printf("Row  %d ", c + 1);
    for (char ch : seatsLeft[c]) {
        System.out.printf("%c ", ch);
    }
    System.out.print("  ");
    for (char ch : seatsRight[c]) {
        System.out.printf("%c ", ch);
    }
    System.out.println();
}