Java度假村费用计算程序:结果展示与客人循环实现求助
问题解决:度假村费用计算程序修正
问题说明
无法从给定的calculateRoomCost、calculateMealPlan、getActivitiesCost方法中提取费用并展示总结果,同时不知道如何根据客人数量进行循环处理,要求必须使用题目提供的方法框架。
现有代码
import java.util.Scanner; public class Problem { static Scanner input = new Scanner(System.in); public static void main(String[] args) { double sum = 0; //display welcome System.out.println("Welcome to Likuliku Lagoon Resort - Malolo Island, Fiji.\n"); System.out.println("Please enter number of guests: "); int guests = input.nextInt(); System.out.println("How many nights will you be staying? "); int GuestNights = input.nextInt(); double loop = getActivitiesCost(); while (loop != 6) { sum = sum + calculateRoomCost() + calculateMealPlan(); loop = getActivitiesCost(); System.out.println(); }//end while loop } public static double calculateRoomCost (int nights){ System.out.println("1: Standard Room no view $100/night"); System.out.println("2: Luxury Room with view $200/night"); System.out.println("3: Luxury Room with Balcony $300/night"); System.out.print("Please enter your choice: "); int cost = input.nextInt(); if (nights == 1) return cost * 100.00; else if (nights == 2) return cost * 200.00; else if (nights == 3) return cost * 300.00; return cost; } public static double calculateMealPlan ( int nights){ System.out.println("1: Lunch & Dinner only $65"); System.out.println("2: 3-Meals a day $150"); System.out.println("3: 3-Meals a day with drinks $225"); System.out.println("4: No meal plan $0"); System.out.print("Please enter your choice: "); int x = input.nextInt(); if (nights == 1) return x * 65.00; else if (nights == 2) return x * 150.00; else if (nights == 3) return x * 225.00; else if (nights == 4) return 0; return nights; } public static double getActivitiesCost () { System.out.println("Choose from these activities"); System.out.println("1: Scuba Adventure $300"); System.out.println("2: Island Shopping Hop $100"); System.out.println("3: Paddle Boarding $125"); System.out.println("4: Deep Sea Fishing $500"); System.out.println("5: Beach Sitting $0"); System.out.println("6: That's all--Done!"); System.out.print("Please enter your choice: "); int loop = input.nextInt(); if (loop == 1) return loop * 300.00; else if (loop == 2) return loop * 100.00; else if (loop == 3) return loop * 125.00; else if (loop == 4) return loop * 500.00; else if (loop == 5) return 0; return loop; } }//end class
预期输入输出
Welcome to Likuliku Lagoon Resort - Malolo Island, Fiji. Please enter number of guests: 2 How many nights will you be staying? 2 Start entering details for guest #1 1 : Standard Room no view $100/night 2 : Luxury Room with view $200/night 3 : Luxury Room with Balcony $300/night Please enter your choice: 1 1 : Lunch & Dinner only $65 2 : 3-Meals a day $150 3 : 3-Meals a day with drinks $225 4 : No meal plan $0 Please enter your choice: 2 Choose from these activities 1 : Scuba Adventure $300 2 : Island Shopping Hop $100 3 : Paddle Boarding $125 4 : Deep Sea Fishing $500 5 : Beach Sitting $0 6 : That's all--Done! Enter your choice: 1 Choose from these activities 1 : Scuba Adventure $300 2 : Island Shopping Hop $100 3 : Paddle Boarding $125 4 : Deep Sea Fishing $500 5 : Beach Sitting $0 6 : That's all--Done! Enter your choice: 6 ------------------------------------------------------------------- Start entering details for guest #2 1 : Standard Room no view $100/night 2 : Luxury Room with view $200/night 3 : Luxury Room with Balcony $300/night Please enter your choice: 2 1 : Lunch & Dinner only $65 2 : 3-Meals a day $150 3 : 3-Meals a day with drinks $225 4 : No meal plan $0 Please enter your choice: 2 Choose from these activities 1 : Scuba Adventure $300 2 : Island Shopping Hop $100 3 : Paddle Boarding $125 4 : Deep Sea Fishing $500 5 : Beach Sitting $0 6 : That's all--Done! Enter your choice: 5 Choose from these activities 1 : Scuba Adventure $300 2 : Island Shopping Hop $100 3 : Paddle Boarding $125 4 : Deep Sea Fishing $500 5 : Beach Sitting $0 6 : That's all--Done! Enter your choice: 6 The total cost estimate $1500.00 The average nightly cost person is $375.
修正后的代码
import java.util.Scanner; public class Problem { static Scanner input = new Scanner(System.in); public static void main(String[] args) { double totalCost = 0; // 显示欢迎信息 System.out.println("Welcome to Likuliku Lagoon Resort - Malolo Island, Fiji.\n"); System.out.println("Please enter number of guests: "); int guestCount = input.nextInt(); System.out.println("How many nights will you be staying? "); int stayNights = input.nextInt(); // 按客人数量循环处理每个客人的费用 for (int i = 1; i <= guestCount; i++) { System.out.println("Start entering details for guest #" + i); // 计算房间费用并累加 totalCost += calculateRoomCost(stayNights); // 计算餐费并累加 totalCost += calculateMealPlan(stayNights); // 处理活动选择,直到用户选6结束 double activityCost; do { activityCost = getActivitiesCost(); // 只有当选择不是6时,才累加活动费用 if (activityCost != 6) { totalCost += activityCost; } System.out.println(); } while (activityCost != 6); // 客人之间的分隔线 if (i != guestCount) { System.out.println("-------------------------------------------------------------------"); } } // 计算人均每晚费用 double avgNightlyPerPerson = totalCost / (guestCount * stayNights); // 格式化输出结果 System.out.printf("The total cost estimate $%.2f\n", totalCost); System.out.printf("The average nightly cost person is $%.0f.\n", avgNightlyPerPerson); } public static double calculateRoomCost(int nights) { System.out.println("1: Standard Room no view $100/night"); System.out.println("2: Luxury Room with view $200/night"); System.out.println("3: Luxury Room with Balcony $300/night"); System.out.print("Please enter your choice: "); int choice = input.nextInt(); // 根据用户选择的房间类型计算总费用(单价 × 入住晚数) switch (choice) { case 1: return 100.00 * nights; case 2: return 200.00 * nights; case 3: return 300.00 * nights; default: return 0; } } public static double calculateMealPlan(int nights) { System.out.println("1: Lunch & Dinner only $65"); System.out.println("2: 3-Meals a day $150"); System.out.println("3: 3-Meals a day with drinks $225"); System.out.println("4: No meal plan $0"); System.out.print("Please enter your choice: "); int choice = input.nextInt(); // 根据用户选择的餐计划计算总费用(单价 × 入住晚数) switch (choice) { case 1: return 65.00 * nights; case 2: return 150.00 * nights; case 3: return 225.00 * nights; case 4: return 0; default: return 0; } } public static double getActivitiesCost() { System.out.println("Choose from these activities"); System.out.println("1: Scuba Adventure $300"); System.out.println("2: Island Shopping Hop $100"); System.out.println("3: Paddle Boarding $125"); System.out.println("4: Deep Sea Fishing $500"); System.out.println("5: Beach Sitting $0"); System.out.println("6: That's all--Done!"); System.out.print("Enter your choice: "); int choice = input.nextInt(); // 根据选择返回对应活动费用,选6则返回6用于终止循环 switch (choice) { case 1: return 300.00; case 2: return 100.00; case 3: return 125.00; case 4: return 500.00; case 5: return 0; case 6: return 6; default: return 0; } } }
关键修改点
- 客人循环处理:使用
for循环遍历每个客人,显示对应提示信息,确保每个客人的费用都被单独计算并累加。 - 修正房间/餐费计算逻辑:原方法将参数
nights和用户选择的选项搞反,现在改为根据用户选择的选项获取单价,再乘以入住晚数得到总费用。 - 活动费用处理:使用
do-while循环让用户可以多次选择活动,直到选6为止,每次选择(除了6)都将活动费用累加到总费用中。 - 结果展示:最后计算并格式化输出总费用和人均每晚费用,符合预期输出格式。
内容的提问来源于stack exchange,提问作者Hello7689
相关产品推荐
相关产品推荐

