2016-11-18 75 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

谢谢。对此,我真的非常感激。 :) –