2017-04-10 54 views
0

這是我的任務,它要求用戶輸入一週中的某一天,它將顯示成本。我希望通過詢問查看者的數量來獲取用戶的更多信息,以便計算票證的總價格。這是我的代碼。計算查看者數量和門票價格

Scanner keyboard = new Scanner(System.in); 
    // Prompt user to enter the day of the week 
    System.out.println("Please enter the day of the week:"); 
    day = keyboard.nextLine(); 

    switch (day){ 
    // User can input the days in different ways and the cost will be printed 
    case "Monday": 
    case "monday": 
    case "MONDAY": 
        System.out.println("The cost of the movie ticket is RM 5."); 
        break; 

    case "Tuesday": 
    case "tuesday": 
    case "TUESDAY": 
        System.out.println("The cost of the movie ticket is RM 5."); 
        break; 

    case "Wednesday": 
    case "wednesday": 
    case "WEDNESDAY": 
        System.out.println("The cost of the movie ticket is RM 5."); 
        break; 

    case "Thursday": 
    case "thursday": 
    case "THURSDAY": 
        System.out.println("The cost of the movie ticket is RM 10."); 
        break; 

    case "Friday": 
    case "friday": 
    case "FRIDAY": 
        System.out.println("The cost of the movie ticket is RM 20."); 
        break; 

    case "Saturday": 
    case "saturday": 
    case "SATURDAY": 
        System.out.println("The cost of the movie ticket is RM 30."); 
        break; 

    case "Sunday": 
    case "sunday": 
    case "SUNDAY": 
        System.out.println("The cost of the movie ticket is RM 20."); 
        break; 

    default: 
      System.out.println("Please make sure you made the correct input."); 
      keyboard.close(); 


    } 

} 

}

+0

而你面對的問題是什麼? –

+0

我對這個問題是什麼感到困惑。另外,只需使用toLowerCase(),這樣就不會複製日子。 – kinezu

+0

它不是問題。我想通過計算總價來添加額外的信息 –

回答

0

雖然我明白,這應該是解決你的問題

Scanner keyboard = new Scanner(System.in); 
// Prompt user to enter the day of the week 
System.out.println("Please enter the day of the week:"); 
//Make the input with lowercase/uppercase so that you don't need to check with many cases 
String day = keyboard.nextLine().toLowerCase(); 
//Input the number of viewers 
System.out.println("Please enter how many viewer are there:"); 
int viewers = keyboard.nextInt(); 
//Define a price 
int price=0; 
switch (day) { 
    // User can input the days in different ways and the cost will be printed 
    case "monday": 
     price=5; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 


    case "tuesday": 
     price=5; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 


    case "wednesday": 
     price=5; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 

    case "thursday": 
     price=10; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 

    case "FRIDAY": 
     price=20; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 


    case "saturday": 
     price=30; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 


    case "sunday": 
     price=20; 
     System.out.println("The cost of the movie ticket is RM"+price+"."); 
     price = price*viewers; 
     System.out.println("The cost of all the movie tickets is RM"+price+"."); 
     break; 

    default: 
     System.out.println("Please make sure you made the correct input."); 
     keyboard.close(); 

} 

請告訴我,如果這是你想要

+0

我星期五應該是星期五 –