我通过使用数组在java中进行了赋值,这里有一些如何编写程序的指令。 我在第2步遇到的问题应该如何初始化数组以显示如下所示的输出(1 A B C D)。
为座位安排编写一个java程序。该计划应具有以下
1 /要求用户输入座位安排中的行数(完成)
2 /根据输入的行,使用数组和循环分配初始座位 编号和显示如下:
Example if you enter ----- > row = 3
The display will be:
1 A B C D
2 A B C D
3 A B C D
3 /要求用户选择他们想要的座位以及所选座位的指定“X”。 (完成) 将要求用户输入,直到给出哨兵值以停止进入。
4 /在指定的“X”
值后显示座位图案 Example if the user choose ----- > 1A 2B 3D
The display will be:
1 X B C D
2 A X C D
3 A B C X
我编写了代码,但是在一个问题中,我遇到了2D数组的问题,我不知道如何初始化数组它是String还是char数组?
我的代码
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter how many row in seating arrangment: ");
int rowNo = s.nextInt();
String seating[][] = new String[rowNo][4];
for (int i = 0; i < rowNo; i++) {
for (int j = 0; j < 4; j++) {
if (j == 0)
seating[i][j] = "A";
else if (j == 1)
seating[i][j] = "B";
else if (j == 2)
seating[i][j] = "C";
else
seating[i][j] = "D";
}
}
for (int i = 0; i < rowNo; i++) {
System.out.println(i + 1);
for (int j = 0; j < 4; j++) {
System.out.println((j + 1) + "\t" + seating[i][j]);
}
System.out.println();
}
String askUser = "Y";
while (askUser.equalsIgnoreCase("Y")) {
System.out.println("Chose your Seat ");
System.out.println("Enter the row ");
int row = s.nextInt();
System.out.println("Enter the column ");
String col = s.nextLine();
if (col.equals("A"))
seating[row - 1][0] = "X";
else if (col.equals("B"))
seating[row - 1][1] = "X";
else if (col.equals("C"))
seating[row - 1][2] = "X";
else
seating[row - 1][3] = "X";
System.out.println("Contnioe to choose seat [y -Yes | n-No] :");
askUser = s.next();
}
System.out.println("Find Seating");
for (int i = 0; i < rowNo; i++) {
for (int j = 0; j < 4; j++) {
System.out.println((j + 1) + "\t" + seating[i][j]);
}
System.out.println();
}
}
}
我的代码输出
Enter how many row in seating arrangment:
3
1
1 A
2 B
3 C
4 D
2
1 A
2 B
3 C
4 D
Chose your Seat
Enter the row
2
Enter the column
A
Contnioe to choose seat [y -Yes | n-No] :
Find Seating
1 A
2 B
3 C
4 D
1 A
2 B
3 C
4 X
我的代码中出现了什么错误? 教师提示:行号应存储在一维字符串数组中。 应该为字符使用2D数组。
我正在寻求帮助。 问候
但是如果我这样做那么2d数组是正确的吗?
String seatRow[]= new String[rowNo];
char [] col= {'A','B','C','D'};
//String[] col= {"a b c d "};
String s=Arrays.toString(col);
String SeatAval[][]= new String [rowNo][s.length()];
我对教师提供的提示设置字符串数组中的行感到困惑。和人物。
答案 0 :(得分:0)
我已经编辑了您的代码并添加了注释,以便您知道出了什么问题。请阅读并理解。此外,您应该添加一些错误检查,以防用户提供错误的输入。这是更新的代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter number of rows in seating arrangment: ");
int rowNo = s.nextInt();
String[][] seating = new String[rowNo][4];
for (int i = 0; i < rowNo; i++) {
for (int j = 0; j < 4; j++) {
if (j == 0)
seating[i][j] = "A";
else if (j == 1)
seating[i][j] = "B";
else if (j == 2)
seating[i][j] = "C";
else
seating[i][j] = "D";
}
}
// Extracted your printing into a method
printSeating(seating);
String askUser = "Y";
while (askUser.equalsIgnoreCase("Y")) {
System.out.println("Chose your Seat ");
System.out.println("Enter the row ");
int row = s.nextInt();
// nextInt() only gets the int and doesn't consume the newline
// so this consumes the newline
s.nextLine();
System.out.println("Enter the column ");
String col = s.nextLine();
if (col.equals("A"))
seating[row - 1][0] = "X";
else if (col.equals("B"))
seating[row - 1][1] = "X";
else if (col.equals("C"))
seating[row - 1][2] = "X";
else
seating[row - 1][3] = "X";
System.out.println("Contnioe to choose seat [y -Yes | n-No] :");
askUser = s.next();
}
System.out.println("Find Seating");
// Extracted your printing into a method
printSeating(seating);
}
private static void printSeating(String[][] seating) {
for (int i = 0; i < seating.length; i++) {
// Don't print a newline, since you want to continue printing on this line
System.out.print(i + 1);
for (int j = 0; j < seating[i].length; j++) {
// Don't print a newline, since you want to continue printing on this line
System.out.print(" " + seating[i][j]);
}
// now you print a newline
System.out.println();
}
}
}