2017-04-02 71 views
0

嗯,我的代碼有點長,所以我只是把第一部分,當你看到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); 

      } 


     } 
+0

注意:'d.equalsIgnoreCase(「A」)'可能更容易。 –

+0

不知道你的意思是不需要任何包裝。但是...... maaaaaaybe考慮在盲目地寫一個'X'之前檢查座位位置是否已經有了'X'? (這是你現在正在做的)。 *(當然,你可以做很多事來擺脫所有重複的代碼,但這不在你的問題的範圍內)* –

回答

0

在將[X]分配給[0] [1]之前,您可以檢查它是否已經是「X」。然後,您可以打印「對不起」

if(x == 1) 
    { 
     if((d.equals("A")|| d.equals("a"))) 
     { 
      if(seat[0][1].equals("X")) 
      { 
      //Print Sorry 
      } 
      else 
      { 
       seat[0][1] = "X"; 
       k.printTable(seat); 
      } 
     } 
    } 
+0

這是一個非常簡單的解決方案,我怎麼可以忘記這一點我的意思是我嘗試了所有可能的使用,例如但它確實有效:D – Ekin

+0

請注意,這是我在答案中提到的確切解決方案,除了編寫OP的代碼。 –

0

很多東西可以做,以清理你的代碼,但是......特別是你的問題,你永遠檢查,看看是否seat[x][y]中已經有一個"X"編寫"X"它之前。

如果您執行一個簡單的if檢查數組的值,並且它已經等於"X",那麼您可以打印消息而不是寫入數組(可能覆蓋先前選擇的座位選擇)。

+0

但是我怎樣才能做到這一點而不使用數組(我在java中是begginer順便) – Ekin

+0

這樣做沒有數組?這不是你問題的一部分。這太寬泛了 - 有很多方法可以做到。 –

+0

是的,這不是我問題的一部分,我只是好奇而已。 – Ekin

0

您需要一個Map數據結構(將數據保存爲鍵值對)來存儲已預訂的座位。

您可以參考解決使用Map您的問題下面的步驟:

(1)當用戶輸入座位號,檢查它是否在Map對象存在

(2)如果存在,即,已經訂好,然後顯示錯誤信息

(3)如果不存在,則允許用戶預定,並存儲在地圖

我已經提供了僞碼的代碼下面,它使用Map

(1)遵循Java的命名約定類名稱以大寫

開始(:

public class Deneme { 

    private static Map<String, String> seatsUsed = new HashMap<>(); 

    public static void main(String[] args) { 

     //add other code here 

     while(x <= 7){ 

      //add other code here 

     if(seatsUsed.get(x+d) != null) { 
      System.out.println(" Sorry, seat alrady booked"); 
     } 

     if(x == 1){ 
      if((d.equals("A")|| d.equals("a"))){ 
       seat[0][1] = "X"; 
       k.printTable(seat); 
       seatsUsed.put(x+d, "Y");//Y indicates that booked 
     } 

     //add other code 

     } 
    } 

此外,作爲一個側面說明,你已經下文提到有很多的你的代碼問題2)你的代碼難以跟進,試圖重構爲更小的方法

(3)利用現有的String /其他API方法有效地喜歡你可以使用equalsIgnoreCase,而不是寫d.equals("A")|| d.equals("a")

相關問題