如何存储用户输入以继续使用?

时间:2022-12-28 15:45:18

How can I repeat this program but keep the user input when it is displayed to them a second time or the third time etc. The program asks them where they want to sit, then the display shows them an X in place of where they said. I want the X to stay for the next time it asks for their input until the user decides to quit the program by choosing "2".

如何重复此程序,但在第二次或第三次等显示给用户时保持用户输入。程序询问他们想要坐的位置,然后显示屏显示X代替他们所说的位置。我希望X在下次询问输入时保持不变,直到用户决定通过选择“2”退出程序。

import java.util.Scanner;
import java.util.Arrays;
class AirplaneSeating {

 static Scanner inNum = new Scanner(System.in);
 static Scanner inStr = new Scanner(System.in);

 static void option() {
  String[][] seatingChart = new String[10][4];
  int rows = 10;
  int columns = 4;

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


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

   }
  }
 public static void main(String[] args) {
   int choice;
   do {
   System.out.println("Choose from one of the following options:");
   System.out.println("\tl. Choose a seat to reserve: ");
   System.out.println("\t2. Quit");
   System.out.print("Enter 1 or 2: ");
   choice = inNum.nextInt();


   switch(choice) {
     case 1:
        option();
        break;
     case 2:
        System.out.println("\nGoodbye!");
        break;
     default:
        System.out.println(choice + " is not an option. Please choose 1, 2, 3, 4, or 5.");                    
  }
  }while(choice !=2);
 }         
}         

1 个解决方案

#1


Use BufferedReaders,BufferedWriters and a .txt file. They're pretty simple to use and allow you to save something in a text document which can later be accessed to reload previous information.

使用BufferedReaders,BufferedWriters和.txt文件。它们使用起来非常简单,允许您在文本文档中保存某些内容,以后可以访问该文档以重新加载以前的信息。

#1


Use BufferedReaders,BufferedWriters and a .txt file. They're pretty simple to use and allow you to save something in a text document which can later be accessed to reload previous information.

使用BufferedReaders,BufferedWriters和.txt文件。它们使用起来非常简单,允许您在文本文档中保存某些内容,以后可以访问该文档以重新加载以前的信息。