2017-10-12 77 views
-3

我正在爲班級任務創建電影票務計劃,並且在工作中遇到了一些麻煩。JAVA:需要幫助修復我的電影票類程序

  1. 我想不出我會如何計算用戶希望,因爲我不知道如何告訴程序使用一個不同的公式VS日場夜票門票的總成本?

  2. 我還需要找到一種方法,不允許用戶購買兒童的門票,如果電影被評爲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); 

} 

}

+4

我已經低估了這個問題,因爲沒有任何對此代碼執行調試的證據。請[編輯]您的問題,向我們展示您的調試已發現的內容,以及關於實際代碼行的具體問題。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)和[如何調試小程序](https://ericlippert.com/2014/03/05 /如何調試的小程序/)。 –

回答

0

我想不出我會如何計算用戶希望因爲機票的總成本我不知道如何告訴該節目使用不同的配方用於日場和夜場門票?

您已經將成本計算在兩個單獨的變量中,因此只需使用條件根據用戶輸入選擇您需要的成本。

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 
} 

一些最後的想法 - 您的程序中有幾個錯誤。我注意到這些,但可能有更多:

  • 總成本的計算是錯誤的。
  • 驗證用戶輸入的邏輯是錯誤的。

請確保您徹底測試了您的代碼。