简体   繁体   English

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

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

I am creating an airplane seating chart. 我正在创建飞机座位表。 I am able to display the chart and can ask for user input. 我能够显示图表,并可以要求用户输入。 I am trying to take what they answer with and put "X" for where their input states. 我正在尝试接受他们的回答,并在其输入状态处输入“ X”。 I don't know how to take their input and display it on the chart and then replace it with 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);      

  }   
}

You just want to display any way? 您只想显示任何方式? How do you want the input? 您想如何输入? Any way? 可以吗

You can just parse the scanner input and tell the user put row, column just like your code. 您只需解析扫描器输入,并告诉用户将行,列放在您的代码中即可。

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

but you will still need a loop to display the seating chart again. 但是您仍然需要循环才能再次显示座位表。

Looking at the your program and the way the seats are structured, you can achieve this by simply parsing and storing the input. 查看您的程序和座位的结构方式,您可以通过简单地解析和存储输入来实现。

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.

Then all you have to do to set the value is is 那么您要做的就是将值设置为

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

Then you can just print it out again. 然后,您可以再次打印出来。

Make sure str is two characters. 确保str是两个字符。 Take the first character (I'm guessing it'll be a alphabetical char), convert it into a number, and store it as a variable (say, x). 取第一个字符(我猜它将是字母字符),将其转换为数字,并将其存储为变量(例如x)。 Do the same with the second character (say, y). 对第二个字符(例如,y)执行相同的操作。 set seatingChart[x][y] = "X" and then run your display loop again. 设置SeatingChart [x] [y] =“ X”,然后再次运行显示循环。

I hope this wasn't too specific of an answer since this is clearly homework. 我希望这不是一个很具体的答案,因为这显然是家庭作业。

Consider creating HashMap<Character, Integer> that maps row letters to the respective number. 考虑创建HashMap<Character, Integer>将行字母映射到相应的数字。 Given user input, take the first character and get the appropriate row index from your hash map. 给定用户输入,获取第一个字符并从哈希映射中获取适当的行索引。 Use library functions to convert the second character to an int . 使用库函数将第二个字符转换为int Now you have indices into your array and can change the value in question. 现在,您已经在数组中添加了索引,并且可以更改相关值。

This snippet could help 该代码段可能会有所帮助

assumed that input is given as the seats are defined. 假设在定义座位时给出输入。 eg: H4 例如: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]

You could initialize this 你可以初始化这个

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

Like this: 像这样:

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

And the answer to your question is that once you have their input line: 问题的答案是,一旦有了他们的输入行:

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

Then you just need to iterate through the array, and find a match: 然后,您只需要遍历数组,并找到一个匹配项:

  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.

相关问题 如何获取用户输入并将其成功存储在 ArrayList 中? 那么我如何让我的程序向我显示 ArrayList 中的所有元素? - How do i take user input and store it successfully in an ArrayList? Then how do i get my program to show me all the elements in the ArrayList? 如何获取用户输入并将其存储到另一个类中的数组中? || 爪哇 - How do I take user input and store it into an array that is in another class? || Java LibGdx-我如何从播放器中获取输入,但又可以从舞台中获取输入 - LibGdx - How do I take input from my player, but also be able to take input from a stage 如何确定我的用户输入是否与 Java 数组中的值之一匹配 - How do I determine if my user input matches one of the values inside my array in Java 如何使用 java 在数组中获取用户输入? - how to take user input in Array using java? 如何获取用户输入并在不同类中的数组列表中搜索用户输入? - How do I take a user input and search through an arraylist in a different class for the user input? 如何获取用户的输入来制作二维数组? - How do take an user's input to make a two-dimensional array? 如何接收用户输入,将其添加到新的扫描仪中,然后将输入扫描到阵列中? - How do I take a users input, add that to a new scanner, and then scan the input into an array? 如何使EditText显示我的文本输入? - How do I make EditText show my text input? 如何更改程序以允许用户输入时间? - How can I change my program to allow user input to take the time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM