Java 计算观众数量和票价
这是我的作业,它要求用户输入一周中的一天,并显示成本。我想从用户那里获得更多信息,询问观众人数,以便计算门票的总定价金额。这是我的密码Java 计算观众数量和票价,java,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.nextLin
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();
}
}
}据我所知,这应该是解决您问题的方法
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();
}
请告诉我这是否是您想要的以及您面临的问题是什么?我不清楚问题是什么。另外,只需使用toLowerCase()即可,这样就不会复制日期。这不是问题。我想通过计算总价格来添加其他信息,您的意思是您想添加类似
numberOfPeople=keyboard.nextLine()的内容代码>?我们可以为您提供解决作业的提示。您不需要在打印的字符串中硬编码成本。大小为7的数组可以容纳成本。当您接受用户提供的观众数量时,将其乘以成本[day_Index],然后打印结果。您也可以使用一个映射来保存成本,其中日期字符串作为键,成本整数作为值。我的坏星期五应该是星期五