java 简单的电影院预订系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40685370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:25:08  来源:igfitidea点击:

Simple Cinema Booking System

java

提问by Rocky Jason

I'm doing a simple cinema booking system. Here's the code that I've done so far. I'm a beginner and I'm pretty sure there are some flaws or bad coding practices in my code so please pardon me and correct me if you would like to.

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

I wanna ask that how can I modify the booked seats from integer to "**" and display it on the seating plan when the second loop of the whole program is executed so that the customers are able to see which seats have been booked? For example, a customer has booked seat 5, and he/she wants to book more, so when the second loop comes, he/she is able to see seat 5 has become **, which means it has been booked. I want to do this using array since I'm learning it, but I appreciate too if you have other methods than using array.

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

回答by Vitthal Kavitake

One suggestion for you.

给你一个建议。

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

You have used too many if elsestatements here. You could use single statement like this

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

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

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

This will minize your code and make it somewhat simpler.

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