如何从头开始修改下面的代码 [英] How do I modify the code below from scratch

查看:86
本文介绍了如何从头开始修改下面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改此代码,如果用户从第一个循环预订座位,那么从第一个循环中取出的座位在第二个循环中不可用?



我尝试过:



How do I modify this code that if the user book the seat from the first loop then the seat taken from the first loop is not available in second loop ?

What I have tried:

import java.util.Scanner;

public class CinemaBooking {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int[] SeatNo = new int[100];

    int Seats;
    int YesOrNo = 1;
    String CustomerName;

    for(int i = 0; i < 12; i++){
    	SeatNo[i]=0;
    }
		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);
    while (YesOrNo == 1) {
      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+1) % 10 == 0 ){
			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();
      int[] takenSeats = {};
      takenSeats.

      while (Seats < 0 || Seats > 100) {
        System.out.println("Only 1 - 100 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.");
        }
      }
    }
  }
}

推荐答案

创建可用座位列表。每次分配一个,将其从列表中删除,因为它不再可用。
Create a list of available seats. Every time you assign one, remove it from the list, as it's no longer available.


这篇关于如何从头开始修改下面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆