我们需要为netbeans的一家本地电影院完成一个简单的票务系统,我陷入了两个问题。
问题1是输出的一部分,一旦您选择了票证类型+数量,就需要输出“您正在购买Y数量的X张票证”
问题2是老年人票需要花费32.50美元,而且我似乎无法找到一种解决方法来允许使用十进制数字进行计算。我进行了调试,似乎将数字更改为整数,然后该整数将无法正确计算。救命!
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);
}
}
答案 0 :(得分:4)
您好,建议进行以下更改。
首先将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()
设置要打印到控制台的消息的格式
答案 1 :(得分:2)
在price1值上,将其存储为int会损失一些精度。
在Java中,将int舍入(舍入)为没有小数点精度的值。为此,您需要使用float或double来保留美分值。
此外,您可能会发现java.text.NumberFormat
类对于货币处理很有用。
答案 2 :(得分:0)
尝试一下
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class testingsystem {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String order, again;
int quantity;
double price1 = 0, price2 = 0, price3 = 0;
int loop1 = 0;
int 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.0;
} else if (order.equalsIgnoreCase("2")) {
price1 = 36.0;
} else if (order.equalsIgnoreCase("3")) {
price1 = 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 " + order + " number of tickets at " + quantity2 + " quantity");
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.printf("Total Price : $%.2f", price2);
}
}