如何获取用户输入并在我的阵列上显示它?

时间:2015-05-06 18:42:44

标签: java arrays

我正在创建一个飞机座位表。我能够显示图表并可以要求用户输入。我正在尝试接受他们回答的内容,并在其输入状态的位置加上“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);      

  }   
}

6 个答案:

答案 0 :(得分:1)

你只想显示任何方式? 你想如何输入?无论如何?

您只需解析扫描仪输入并告诉用户将行,列放入您的代码中。

//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"

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

答案 1 :(得分:1)

查看您的程序和座位的结构方式,您只需解析并存储输入即可实现此目的。

char firstChar = str.toUpperCase().charAt(0);
char secondChar = str.charAt(1);
int first = ((int) firstChar) - 101; // ascii value - 101 for upper case A
int second = ((int) secondChar) - 1; // cast the character to an int, then -1 because it's 0 indexed.

然后你要做的就是设置值是

seatingChart[first][second] = "X";

然后你可以再打印出来。

答案 2 :(得分:0)

确保str是两个字符。取第一个字符(我猜它将是一个按字母顺序排列的字符),将其转换为数字,并将其存储为变量(例如,x)。对第二个字符(例如,y)执行相同操作。设置seatingChart [x] [y] =“X”,然后再次运行显示循环。

我希望这不是一个特定的答案,因为这显然是家庭作业。

答案 3 :(得分:0)

考虑创建将行字母映射到相应数字的HashMap<Character, Integer>。给定用户输入,获取第一个字符并从哈希映射中获取适当的行索引。使用库函数将第二个字符转换为int。现在你有了数组的索引,可以改变有问题的值。

答案 4 :(得分:0)

此代码段可以提供帮助

假设在定义席位时给出输入。例如:H4

Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int x = (int)input.charAt(0)-65; //since 'A' has ASCII value of 65
int y = (int)input.charAt(1)-49; //since '0' has ASCII value of 49
// update seatingChart[x][y]

答案 5 :(得分:0)

您可以初始化

  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";

像这样:

    String[][] seatingChart = new String[rows][columns];
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < columns; j++) {
            seatingChart[i][j] = "" + ((char)('A' + i)) + ((char)('1' + j));
        }
    }

问题的答案是,一旦你有输入线:

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

然后你只需要遍历数组,找到一个匹配:

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