我正在为课堂作业创建一个电影票务计划,并且在完成一些工作时遇到了一些麻烦。
我无法弄清楚我将如何计算用户想要的门票总费用,因为我不知道如何告诉程序使用不同的matinee vs公式晚票?
如果电影评级为R,我还需要找到一种不允许用户购买儿童票的方法吗?
我的代码如下......
/**
* @param args
*/
public static void main(String[] args)
{
//initializes variables for the various costs of each ticket
double adultMatCost = 7.50;
double adultEveCost = 9.50;
double childMatCost = 7.00;
double childEveCost = 7.00;
double seniorMatCost = 5.00;
double seniorEveCost = 7.00;
//introduces the user to the program, creates scanner for program,
//and asks user to enter their name
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the J-Town Theater.");
System.out.print("Please enter a name for our transaction records:");
String name = scan.next();
//thanks user and asks user which movie they would like to choose
//assigns a String movie is assigned text based on what the user chose
//if user enters a value other than 1 or 2 the program terminates gracefully
System.out.println("Thank you, " + name);
System.out.println("Which movie would you like to see?");
System.out.println("[1] My Little Pony (PG)");
System.out.println("[2] Blade Runner 2049 (R)");
System.out.print("Enter movie choice: ");
int movieChoice = scan.nextInt();
String movie = "";
if (movieChoice == 1){
movie = ("My Little Pony");
}
else if (movieChoice == 2){
movie = ("Blade Runner 2049");
}
else{
System.out.println("I'm sorry, " + name + ", but that is an invalid choice");
System.out.println("Goodbye.");
return;
}
//asks user what time they want to see the movie
//user enters M/m for Matinee and E/e for evening
//if user enters something besides M/m/E/e the program terminates gracefully
System.out.println("What time of day would you like?");
System.out.println("[M] Matinee");
System.out.println("[E] Evening");
System.out.print("Enter movie time:");
String timeDay = scan.next();
double totalCost = 0;
String timeDayResult = "";
double timeDayME = 0;
if (timeDay.equals("m")){
timeDayResult = ("Matinee showing");
}
else if (timeDay.equals("M")){
timeDayResult = ("Matinee showing");
}
else if (timeDay.equals("e")){
timeDayResult = ("Evening showing");
}
else if (timeDay.equals("E")){
timeDayResult = ("Evening showing");
}
else{
System.out.println("I'm sorry, " + name + ", but that is an invalid choice.");
System.out.println("Goodbye.");
return;
}
//asks how many tickets they would like and shows the price for each ticket
//user enters number between 0 and 10 if they enter something besides that
//the program will terminate gracefully
//if the movie chosen was rated R no tickets can be sold to children
System.out.println("How many tickets would you like?");
System.out.println("Child : $7.00");
System.out.println("Adult : $7.50");
System.out.println("Senior: $5.00");
System.out.print("Enter number of children (0-10):");
int ticketsChild = scan.nextInt();
if (ticketsChild >= 0){
int ticketsChildTotal = ticketsChild;
}
if (ticketsChild <= 10){
int ticketsChildTotal = ticketsChild;
}
else{
System.out.println("I'm sorry, " + name + ", but that is an invalid choice.");
System.out.println("Goodbye.");
return;
}
System.out.print("Enter number of adults (0-10):");
int ticketsAdult = scan.nextInt();
if (ticketsAdult >= 0){
int ticketsAdultTotal = ticketsAdult;
}
if (ticketsAdult <= 10){
int ticketsAdultTotal = ticketsAdult;
}
else{
System.out.println("I'm sorry, " + name + ", but that is an invalid choice.");
System.out.println("Goodbye.");
return;
}
System.out.print("Enter number of seniors (0-10):");
int ticketsSenior = scan.nextInt();
if (ticketsSenior >= 0){
int ticketsSeniorTotal = ticketsSenior;
}
if (ticketsSenior <= 10){
int ticketsSeniorTotal = ticketsSenior;
}
else{
System.out.println("I'm sorry, " + name + ", but that is an invalid choice.");
System.out.println("Goodbye.");
return;
}
//gives a transaction summary for the user showing what movie they want,
//what time they want, and how many tickets they want total,
//then the program calculates the total ticket price
System.out.println("Thank you, Penolope, here is a record of your purchase.");
int totalTickets = ticketsSenior + ticketsAdult + ticketsChild;
System.out.println(movie + " (" + timeDayResult + "): " + totalTickets + " tickets total");
System.out.println("Children: " + ticketsChild);
System.out.println("Adults: " + ticketsAdult);
System.out.println("Seniors: " + ticketsSenior);
double totalMatineeCost = (childMatCost * ticketsChild) + (adultMatCost * ticketsAdult) + (seniorMatCost + ticketsSenior);
double totalEveningCost = (childMatCost * ticketsChild) + (adultMatCost * ticketsAdult) + (seniorMatCost + ticketsSenior);
System.out.println("Total cost: $" + totalCost);
}
}
答案 0 :(得分:0)
我无法弄清楚我将如何计算用户想要的门票的总费用,因为我不知道如何告诉程序使用不同的matinee与晚间门票的公式?
您已经在两个单独的变量中计算了成本,因此只需使用条件根据用户输入选择您需要的那个。
if("Matinee showing".equals(timeDayResult)){
totalCost = totalMatineeCost;
}else{
totalCost = totalEveningCost;
}
如果电影评级为R,我还需要找到一种不允许用户购买儿童票的方法吗?
再次,在询问孩子数量之前使用条件。类似的东西:
if(movieChoice == 2){
//Set number of child tickets to 0, perhaps inform user they are not allowed to buy child tickets since movie is rated R
}else{
//ask the user for number of child tickets
}
最后一些想法 - 你的程序中有几个错误。我注意到了这些,但可能还有更多:
确保彻底测试您的代码。