简单的电影预订系统

时间:2016-11-18 20:22:40

标签: 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.");
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

给你一个建议。

var foo = Promise.resolve(1234);
foo.then(function (result) {
    console.log(result); //-> 1234;
    return new Promise(function(resolve) {
        setTimeout(function() {
            resolve(5678);
        }, 1000);
    });
}).then(function (result) {
    console.log(result); //-> 5678
    return new Promise(function(resolve) {
        setTimeout(function() {
            resolve("BLAGH");
        }, 1000);
    });
}).then(function (result) {
    console.log(result); //-> "BLAGH"
    return new Promise(function(resolve) {
        setTimeout(function() {
            resolve();
        }, 1000);
    });
}).then(function (result) {
    console.log(result); //-> undefined
});

您在此处使用了太多if ( SeatCounter == 4) else if ( SeatCounter == 9) else if ( SeatCounter == 14 ) ... 语句。您可以使用像这样的单一语句

if else

这会缩小您的代码并使其更简单。