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

时间:2015-05-07 00:01:10

标签: 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);
 }         
}         

1 个答案:

答案 0 :(得分:0)

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