我目前正在完成一个简单的票务系统的作业,但对于如何正确计算循环我感到困惑。
基本上,该程序将计算在本地电影院购买门票的总费用,但是,当用户输入特定的TicketType和数量时,循环将重置,用户将输入第二个TicketType和数量,并且此过程将重复多次,直到用户切下循环为止,并显示所有循环的总计算成本。
我在这里和亲自获得了代码方面的帮助,而对于为什么此循环未正确循环,我感到很困惑!单个循环计算完美,但是第二个循环失败。
我目前正在尝试使用枚举类,该类已协助解决票务计算中的其他问题。
package ticketingsystem
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ticketingsystem {
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String order, again;
int quantity = 0;
double totalPrice;
totalPrice = 0;
TicketType ticketType;
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();
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;
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);
}
}
我也将门票价格链接到枚举类,详细信息如下:
package ticketingsystem;
/**
*
* @author
*/
enum TicketType {
child(18), adult(36), senior(32.5);
TicketType(double price) {
this.price = price;
}
private double price;
public double getPrice() {
return price;
}
}
期望的输出是要重复无数次的循环,同时保留先前计算出的数字,只要用户需要就可以。
没有错误消息,但是在第二个循环中输入票证类型和数量后,系统没有输出。
答案 0 :(得分:2)
FWIW,我也刚刚尝试了您最初发布的问题中的代码。
您问题中的代码唯一的错误是在public class ticketingsystem
主类的package语句中缺少分号。
这是一个错误,这意味着编译时它会失败并且仅运行代码的先前版本。这可能就是为什么您没有获得预期的结果(您正在运行旧版本)的原因。
这是我使用您确切的代码的会话记录(在主类的package语句上加上分号):
Welcome to the cinemas!
MAIN MENU
The cinema has the following options
1 = Child (4-5 yrs)
2 = Adult (18+ yrs)
3 = Senior (60+ yrs)
Enter your option: 1
Enter the number of tickets: 2
You are purchasing child tickets at 18.00
Do you wish to continue? (Y/N) : y
Enter your option: 2
Enter the number of tickets: 3
You are purchasing adult tickets at 36.00
Do you wish to continue? (Y/N) : n
Total Price : $144.00
昂贵的电影院!
FWIW,正如R. Katnaan所说,您必须遵循语法规则。但是,枚举元素名称的大写字母不是该语言的规则。这是一个高度提倡的约定(编码标准)。我同意您应该对您的枚举元素使用大写字母,但这并不是您遇到问题时破坏代码的地方...
如果遵循该编码约定,则还应该将Capital CamelCase用于类(接口和枚举名称)及其他。例如,使用public class TicketingSystem
而不是public class ticketingsystem
。以下是一些有关命名约定的指南。请记住,这些是指南,而不是编译器强制执行的规则:
这些约定很重要,因为它们为您的代码读者提供了一些有关它们可能正在寻找的线索。例如,如果我看到x = SOME_VALUE;
,我将假定x被分配一个常量,而不是x = someValue;
,我将假定x被分配了另一个变量的值。等等...
答案 1 :(得分:1)
运行它时,我似乎没有在您的代码中发现任何问题。当我运行您的代码时,请参见下面的输出,它计算得很好:
Welcome to the cinemas!
MAIN MENU
The cinema has the following options
1 = Child (4-5 yrs)
2 = Adult (18+ yrs)
3 = Senior (60+ yrs)
Enter your option: 2
Enter the number of tickets: 2
You are purchasing adult tickets at 36.00
Do you wish to continue? (Y/N) : Y
Enter your option: 3
Enter the number of tickets: 1
You are purchasing senior tickets at 32.50
Do you wish to continue? (Y/N) : Y
Enter your option: 1
Enter the number of tickets: 1
You are purchasing child tickets at 18.00
Do you wish to continue? (Y/N) : N
Total Price : $122.50
答案 2 :(得分:0)
当我尝试使用以下代码时,一切正常。
package mm.com.java.so.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TicketingSystem {
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 type, again;
int quantity = 0;
double totalPrice = 0;
TicketType ticketType;
System.out.println("Welcome to the cinemas!\n");
System.out.println("MAIN MENU\n");
System.out.println("The cinema has the following options\n");
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.print("\nEnter your option: ");
type = br.readLine();
switch (type) {
case "1":
ticketType = TicketType.CHILD;
break;
case "2":
ticketType = TicketType.ADULT;
break;
default:
ticketType = TicketType.SENIOR;
break;
}
System.out.print("Enter the number of tickets: ");
quantity = Integer.parseInt(br.readLine());
totalPrice += ticketType.getPrice() * quantity;
System.out.printf("--> You are purchasing %s - %s Ticket(s) at $%s\n", quantity, ticketType, ticketType.getPrice());
System.out.print("\nDo you wish to continue? (Y/N) : ");
again = br.readLine();
} while (again.equalsIgnoreCase("y"));
System.out.printf("\n==> Total Price : $%s \n", totalPrice);
}
}
您可以在这里查看我的测试结果。
Welcome to the cinemas!
MAIN MENU
The cinema has the following options
1 = Child (4-5 yrs)
2 = Adult (18+ yrs)
3 = Senior (60+ yrs)
Enter your option: 1
Enter the number of tickets: 1
--> You are purchasing 1 - CHILD Ticket(s) at $18.0
Do you wish to continue? (Y/N) : y
Enter your option: 2
Enter the number of tickets: 2
--> You are purchasing 2 - ADULT Ticket(s) at $36.0
Do you wish to continue? (Y/N) : y
Enter your option: 3
Enter the number of tickets: 3
--> You are purchasing 3 - SENIOR Ticket(s) at $32.5
Do you wish to continue? (Y/N) : n
==> Total Price : $187.5
注意::如果您是JAVA开发人员,则在编写代码时(例如类名)必须遵循JAVA规则和规定。我格式化了您的代码。请仔细检查。
此代码在我的PC上正常运行。