2016-11-18 114 views
0

我正在做一個簡單的電影預約系統。以下是我迄今爲止所做的代碼。我是一名初學者,我很確定我的代碼中存在一些缺陷或不好的編碼習慣,所以請原諒我,如果您願意,請糾正我。簡易電影預約系統

我想問一下,如何執行整個程序的第二個循環,以便客戶能夠看到哪些座位有空位時,如何將整數預訂座位從「整數」修改爲「**」並顯示在座位圖上已被預訂?例如,一個顧客已經預訂了座位5,並且他/她想要預訂更多,所以當第二個環路到來時,他/她能夠看到座位5已經變成了**,這意味着它已經被預訂了。我想使用數組來完成這個任務,因爲我正在學習它,但是如果你有其他方法而不是使用數組,我也會很感激。

import java.util.Scanner; 

public class CinemaBooking { 

    public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    int[] SeatNo = new int[30]; 
    int Seats; 
    int YesOrNo = 1; 
    String CustomerName; 

    while (YesOrNo == 1) { 
     System.out.print("Welcome to Crazy Cinema!\nWhat is your name?\n"); 
     CustomerName = input.nextLine(); 

     System.out.printf("Welcome %s! Please have a look at the seating plan.\n\n", CustomerName); 

     for (int i = 1; i <= 34; i++) { 
     System.out.print("*"); 
     } 
     System.out.println(); 

     System.out.print("  CINEMA 1 SEATING PLAN"); 
     System.out.println(); 

     for (int j = 1; j <= 34; j++) { 
     System.out.print("*"); 
     } 
     System.out.println(); 

     for (int SeatCounter = 0; SeatCounter < SeatNo.length; SeatCounter++) { 
     System.out.printf(SeatCounter + "\t"); 

     if (SeatCounter == 4) { 
      System.out.println(); 
     } else if (SeatCounter == 9) { 
      System.out.println(); 
     } else if (SeatCounter == 14) { 
      System.out.println(); 
     } else if (SeatCounter == 19) { 
      System.out.println(); 
     } else if (SeatCounter == 24) { 
      System.out.println(); 
     } else if (SeatCounter == 29) { 
      System.out.println(); 
     } 
     } 
     for (int k = 1; k <= 34; k++) { 
     System.out.print("*"); 
     } 
     System.out.println(); 

     System.out.print("Which seat would you like to book? "); 
     Seats = input.nextInt(); 

     while (Seats < 0 || Seats > 29) { 
     System.out.println("Only 0 - 29 seats are allowed to book. Please try again: "); 
     Seats = input.nextInt(); 
     } 

     for (int SeatCounter = 0; SeatCounter < SeatNo.length; SeatCounter++) { 
     if (SeatCounter == Seats) { 
      System.out.println("Seat " + Seats + " is successfully booked."); 
      System.out.println(
       "Thanks for booking!\n\nWould you like to make next booking? (Type 1 = Yes; Type 2 = No)"); 
      YesOrNo = input.nextInt(); 

      if (YesOrNo == 2) { 
      System.out.println("Thank you for using this program."); 
      } 
     } 
     } 

     while (YesOrNo != 1 && YesOrNo != 2) { 
     System.out.println("Invalid input."); 
     System.out.println("Type 1 = Continue booking; Type 2 = Exit the program"); 
     YesOrNo = input.nextInt(); 

     if (YesOrNo == 2) { 
      System.out.println("Thank you for using this program."); 
     } 
     } 
    } 
    } 
} 
+3

「錯誤的編碼實踐」最明顯的錯誤編碼實踐是糟糕的格式。學習縮進你的代碼(或者弄清楚如何讓你的IDE爲你做);它會爲你和他人理解你的代碼創造奇蹟。 –

+1

下一個最明顯的是命名:變量應該以小寫字母開頭;類以大寫字母開頭。請參閱[Oracle的約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html)和[Google的Java風格指南](https://google.github.io/styleguide/javaguide.html #S5命名)。 –

+0

謝謝。感謝你的幫助。 :)我會解決它。 –

回答

1

對您有一個建議。

if (SeatCounter == 4) 
else if (SeatCounter == 9) 
else if (SeatCounter == 14) 
... 

您在這裏使用的語句太多if else。你可以使用單個語句這樣

if((SeatCount+1) % 5 == 0)

這將minize你的代碼,使之略爲簡單。

+0

謝謝。對此,我真的非常感激。 :) –