gpt4 book ai didi

java - 如何获取用户输入并将其显示在我的阵列上?

转载 作者:行者123 更新时间:2023-12-02 04:41:53 26 4
gpt4 key购买 nike

我正在创建飞机座位表。我能够显示图表并可以要求用户输入。我试图接受他们的回答,并用“X”表示他们的输入状态。我不知道如何获取他们的输入并将其显示在图表上,然后将其替换为 X。

import java.util.Scanner;

class AirplaneSeating {
public static void main(String[] args) {
String[][] seatingChart = new String[10][4];
int rows = 10;
int columns = 4;

Scanner inStr = new Scanner(System.in);

seatingChart = new String[rows][columns];

seatingChart[0][0] = "A1";
seatingChart[0][1] = "A2";
seatingChart[0][2] = "A3";
seatingChart[0][3] = "A4";
seatingChart[1][0] = "B1";
seatingChart[1][1] = "B2";
seatingChart[1][2] = "B3";
seatingChart[1][3] = "B4";
seatingChart[2][0] = "C1";
seatingChart[2][1] = "C2";
seatingChart[2][2] = "C3";
seatingChart[2][3] = "C4";
seatingChart[3][0] = "D1";
seatingChart[3][1] = "D2";
seatingChart[3][2] = "D3";
seatingChart[3][3] = "D4";
seatingChart[4][0] = "E1";
seatingChart[4][1] = "E2";
seatingChart[4][2] = "E3";
seatingChart[4][3] = "E4";
seatingChart[5][0] = "F1";
seatingChart[5][1] = "F2";
seatingChart[5][2] = "F3";
seatingChart[5][3] = "F4";
seatingChart[6][0] = "G1";
seatingChart[6][1] = "G2";
seatingChart[6][2] = "G3";
seatingChart[6][3] = "G4";
seatingChart[7][0] = "H1";
seatingChart[7][1] = "H2";
seatingChart[7][2] = "H3";
seatingChart[7][3] = "H4";
seatingChart[8][0] = "I1";
seatingChart[8][1] = "I2";
seatingChart[8][2] = "I3";
seatingChart[8][3] = "I4";
seatingChart[9][0] = "J1";
seatingChart[9][1] = "J2";
seatingChart[9][2] = "J3";
seatingChart[9][3] = "J4";

for(int i = 0; i < rows ; i++) {
for(int j = 0; j < columns ; j++) {
System.out.print(seatingChart[i][j] + " ");
}
System.out.println("");
}

System.out.println("What seat would you like to reserve? ");
String str = inStr.nextLine();

System.out.println("You chose: " + str);

}
}

最佳答案

你只是想以任何方式显示?你想要怎样的输入?有办法吗?

您可以解析扫描仪输入并告诉用户输入行、列,就像您的代码一样。

//Example: Input: 6,2
//Split the user input
String[] input = str.split(",");
//parse first number to make it the row
int rowuser = Integer.parseInt(input[0].trim());
//parse second number to make it the column
int columnuser = Integer.parseInt(input[1].trim());
//replace with "X" value the seatingChart[row][column]
seatingChart[rowuser][columnuser] = "X"

但您仍然需要一个循环来再次显示座位表。

关于java - 如何获取用户输入并将其显示在我的阵列上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30085052/

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