嗯,我的代碼有點長,所以我只是把第一部分,當你看到x == 1部分實際上它有七個這樣的部分。我的目標就像你選擇1A,它給你表(我做的那一部分)。我想要的是,如果用戶再次寫1A,系統必須說抱歉。我怎樣才能不使用任何軟件包?用戶詢問兩次,第二次用戶得到不同的答案?
import java.util.Scanner;
class deneme{
public void printTable(String [ ][ ] seat){
for(int r=0; r<seat.length; r++){
for(int c = 0; c< seat[0].length; c++){
System.out.print(seat[r][c]);
}
System.out.println();
}
}
public static void main(String[] args) {
deneme k = new deneme();
String [][] seat = {{"1","A","B","C","D"},{"2","A","B","C","D"},{"3","A","B","C","D"},{"4","A","B","C","D"},{"5","A","B","C","D"},{"6","A","B","C","D"},{"7","A","B","C","D"}};
Scanner read = new Scanner(System.in);
int x = 6;
String s;
String d;
while(x <= 7){
System.out.println("Please choose your seat number (1 to 7).But if you want to terminate the program please write 0");
s = read.nextLine();
x = Integer.parseInt(s);
System.out.println("Please choose your seat row (A to D)");
d = read.nextLine();
if(x == 1){
if((d.equals("A")|| d.equals("a"))){
seat[0][1] = "X";
k.printTable(seat);
}
else if((d.equals("B")|| d.equals("b"))){
seat[0][2] = "X";
System.out.println("Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("C")|| d.equals("c"))){
seat[0][3] = "X";
System.out.println(" Your seat is chosen ");
k.printTable(seat);
}
else if((d.equals("D")|| d.equals("d"))){
seat[0][4] = "X";
System.out.println(" Your seat is chosen ");
k.printTable(seat);
}
}
注意:'d.equalsIgnoreCase(「A」)'可能更容易。 –
不知道你的意思是不需要任何包裝。但是...... maaaaaaybe考慮在盲目地寫一個'X'之前檢查座位位置是否已經有了'X'? (這是你現在正在做的)。 *(當然,你可以做很多事來擺脫所有重複的代碼,但這不在你的問題的範圍內)* –