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

标签 java arrays

我怎样才能重复这个程序,但在第二次或第三次等向他们显示时保留用户输入。程序询问他们想坐在哪里,然后显示屏会在他们所说的位置显示一个 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);
 }         
}         

最佳答案

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

关于java - 如何存储用户输入以供继续使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30089701/

相关文章:

java - 如何使用 cURL 发送带有请求参数的 POST 请求?

java - Hibernate 提示@OneToOne 中的空@Id,即使它不是空的

c - 如何计算整数数组中的元素数

C 使用函数计算阶乘

Java泛型——获取泛型参数的实际类型

java - Gradle基于环境属性构建War文件

java - 2个本地Java应用程序如何交互?

javascript - 测试文本区域中的无效文本格式

javascript - 排序后替换数组中的值

Javascript 数组未定义......我不确定为什么