计算观众人数和门票价格

时间:2017-04-10 05:31:01

标签: java

这是我的作业,它要求用户输入一周中的某一天,它将显示费用。我想通过询问查看者的号码来获取用户的更多信息,以便计算票证的总定价金额。这是我的代码。

    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();


    }   

}

}

1 个答案:

答案 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();

}

请告诉我这是否是你想要的