gpt4 book ai didi

java - 初始化 2d 字符数组以测试打印时出错

转载 作者:行者123 更新时间:2023-11-30 09:01:03 26 4
gpt4 key购买 nike

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

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[][] seatLeft = 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("");
}

**编辑:**

它打印出来是这样的:A B C D E F Row 1 [C@55f96302 [C@55f96302 [C@55f96302 [C@3d4eac69 [C@3d4eac69 [C@3d4eac69 Row 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] 返回您所看到的那一行的地址。

关于java - 初始化 2d 字符数组以测试打印时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26510318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com