java - 简单的电影院预订系统

标签 java

我正在做一个简单的电影预订系统。这是我到目前为止所做的代码。我是一个初学者,我很确定我的代码中存在一些缺陷或不良的编码实践,因此如果您愿意,请原谅我并纠正我。

我想问一下,如何将已预订的座位从整数修改为“**”,并在执行整个程序的第二个循环时将其显示在座位表上,以便客户能够看到哪些座位已被预订预订了?例如,一位顾客已经预订了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.");
        }
      }
    }
  }
}

最佳答案

给你一个建议。

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

您在这里使用了太多 if else 语句。您可以使用这样的单个语句

if( (座位数+1) % 5 == 0 )

这将最小化您的代码并使其更加简单。

关于java - 简单的电影院预订系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40685370/

相关文章:

java - Firebase 和 Android Studio,无法通过 : W/SyncTree: Listen at/message failed: DatabaseError: Permission denied

java - 基于唯一子字符串对字符串

java - XSLT 或 JAXB 使用哪一个

java - List<String> 作为 GET 方法的输入

java - 任务总数小于分配的线程数时的 ExecutorService 行为

java - 如何在java中转换 "' "to " ` "

java - 开始探索复杂的 java 项目

java - 如何解决 org.openqa.selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document

java - 更改在我的 ActionListener 之后声明的标签文本

java - 序列化:java.io.StreamCorruptedException:无效的流 header :0AACED00