我们需要在 netbeans 中为本地电影院完成一个简单的票务系统,我遇到了两个问题。
package ticketingsystem;
import java.io.*;
public class ticketingsystem
{
public static void main(String []args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String order,again;
int quantity,price1=0,price2=0, price3=0,loop1=0,quantity1,quantity2=0;
System.out.println(" ");
System.out.println("Welcome to the cinemas!");
System.out.println(" ");
System.out.println("MAIN MENU");
System.out.println(" ");
System.out.println("The cinema has the following options");
System.out.println(" ");
System.out.println("1 = Child (4-5 yrs)");
System.out.println("2 = Adult (18+ yrs)");
System.out.println("3 = Senior (60+ yrs)");
do{
System.out.println(" ");
System.out.print("Enter your option: ");
order=br.readLine();
if (order.equalsIgnoreCase("1")) {
price1=18;
} else if (order.equalsIgnoreCase("2")) {
price1=36;
}
else if (order.equalsIgnoreCase("3")) {
price1= (int) 32.5;
}
System.out.print("Enter the number of tickets: ");
quantity1= Integer.parseInt(br.readLine());
quantity2=quantity1+quantity2;
price2=price1*quantity2;
System.out.println("You are purchasing " int (order=br) " tickets at" (quantity1);
System.out.print("Do you wish to continue? (Y/N) : ");
again=br.readLine();
if (again.equalsIgnoreCase("y"))
loop1=loop1+1;
else loop1=loop1-100;
} while (loop1==1);
System.out.println(" ");
System.out.println("Total Price : "+price2);
}
}
最佳答案
您好,建议进行以下更改。
首先将 price2 重命名为 totalPrice 并将其更改为 double :double totalPrice;
我将为 TicketType 创建一个枚举类,您还可以在其中分配 price1 值:
enum TicketType {
child(18), adult(36), senior(32.5);
TicketType(double price) {
this.price = price;
}
private double price;
public double getPrice() {
return price;
}
}
您现在可以将主要方法更改为:
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String order, again;
int quantity = 0;
double totalPrice;
TicketType ticketType; // add the enum to the method
System.out.println(" ");
System.out.println("Welcome to the cinemas!");
System.out.println(" ");
System.out.println("MAIN MENU");
System.out.println(" ");
System.out.println("The cinema has the following options");
System.out.println(" ");
System.out.println("1 = Child (4-5 yrs)");
System.out.println("2 = Adult (18+ yrs)");
System.out.println("3 = Senior (60+ yrs)");
do {
System.out.println(" ");
System.out.print("Enter your option: ");
order = br.readLine();
//use a switch case instead of the if else pattern
switch (order.toLowerCase()) {
case "1":
ticketType = TicketType.child;
break;
case "3":
ticketType = TicketType.senior;
break;
default:
ticketType = TicketType.adult;
break;
}
System.out.print("Enter the number of tickets: ");
quantity = Integer.parseInt(br.readLine());
totalPrice += ticketType.getPrice() * quantity;
// %s will be replaced with the string value of ticketType, %.2f means it will be replaced with a decimal, round to decimals
System.out.printf("You are purchasing %s tickets at %.2f \n", ticketType, ticketType.getPrice());
System.out.print("Do you wish to continue? (Y/N) : ");
again = br.readLine();
}
while (again.equalsIgnoreCase("y"));
System.out.println(" ");
System.out.printf("Total Price : $%.2f \n", totalPrice);
}
您可以使用
System.out.printf()
来格式化您打印到控制台的消息关于java - netbeans 中的简单 Java 票务系统,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56110612/