我的飞机座位表无法正确打印。仅当输入预初始化的数组时,我才会收到一长串错误。我不确定我目前在做什么错,但是它不起作用。
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
最佳答案
你需要这样做
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]&seatRight [c2]返回您所看到的那一行的地址。