1
如何重複此程序,但在第二次或第三次向他們顯示時保留用戶輸入等。程序詢問他們想要坐的位置,然後顯示器向他們顯示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);
}
}
你必須從某處加載它,例如。文件,文本文件,服務器。因此,創建一個帶有2個值的.txt文件非常簡單...如果它是一個類,並且您想要將它加載到您應該查看Serializable –