繁体   English   中英

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

[英]How do I take user input and show it on my array?

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

但是您仍然需要循环才能再次显示座位表。

查看您的程序和座位的结构方式,您可以通过简单地解析和存储输入来实现。

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

然后,您可以再次打印出来。

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

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

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

该代码段可能会有所帮助

假设在定义座位时给出输入。 例如: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]

你可以初始化这个

  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("");
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM