
1. 引言
在开发交互式应用程序时,经常需要从用户那里收集多组数据,并对这些数据进行处理和汇总。本教程将以一个度假村费用计算系统为例,演示如何在java中有效地处理多位客人的输入、利用方法(methods)进行模块化计算,并将这些计算结果准确地累加和显示给用户。我们将重点解决如何为每位客人循环收集信息,以及如何从各个计算方法中获取并汇总费用。
2. 核心问题与解决方案
原始代码面临的主要挑战是:
- 如何为多位客人循环收集信息? 现有的 main 方法只处理了一次输入流程,无法根据用户输入的客人数量进行重复。
- 如何将方法计算的结果传递并累加到总费用中? calculateRoomCost 和 calculateMealPlan 方法需要 nights 参数,但在 main 方法中调用时未传递。同时,getActivitiesCost 的结果也未被正确累加。
- 如何最终显示汇总的总费用和平均费用?
针对这些问题,我们将采用以下解决方案:
- 使用 for 循环来迭代处理每位客人的数据。
- 确保方法调用时传递正确的参数。
- 引入一个累加变量 sum 来收集所有费用。
- 在循环结束后,统一显示计算结果。
3. 系统架构与方法详解
该系统由一个主方法 main 和三个辅助计算方法组成:
- calculateRoomCost(int nights): 根据用户选择的房间类型和入住晚数计算房间费用。
- calculateMealPlan(int nights): 根据用户选择的餐饮计划和入住晚数计算餐饮费用。
- getActivitiesCost(): 根据用户选择的活动计算活动费用。
所有方法都使用一个全局的 Scanner 对象 input 来获取用户输入。
立即学习“Java免费学习笔记(深入)”;
3.1 main 方法的优化
main 方法是整个程序的入口和控制中心。为了实现多客人数据处理和费用汇总,我们需要对其进行以下关键修改:
- 客人数量循环: 使用 for 循环,根据用户输入的 guests 数量进行迭代。每次迭代代表一位客人的信息录入。
- 费用累加: 引入一个 double 类型的变量 sum,初始化为 0,用于累加所有客人的房间费、餐饮费和活动费。
- 方法参数传递: 调用 calculateRoomCost 和 calculateMealPlan 时,必须传入用户输入的 GuestNights 参数。
- 活动费用处理: getActivitiesCost 方法不仅返回活动费用,其返回值 6 还作为退出活动选择循环的标志。因此,在累加活动费用时,需要判断返回值是否为 6,避免将退出标志误算为费用。
以下是优化后的 main 方法代码:
import java.util.Scanner;
public class Problem {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
double sum = 0; // 初始化总费用累加器
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(); // 获取入住晚数
// 循环处理每位客人的信息
for (int i = 0; i < guests; i++) {
System.out.println("Start entering details for guest #" + (i + 1));
// 计算房间和餐饮费用,并累加到总和
// 注意:这里需要将 GuestNights 作为参数传递给方法
sum = sum + calculateRoomCost(GuestNights) + calculateMealPlan(GuestNights);
double activityLoopResult = 0; // 用于存储每次活动选择的结果
// 循环选择活动,直到用户选择“完成” (6)
while (activityLoopResult != 6) {
activityLoopResult = getActivitiesCost(); // 获取活动选择和费用
// 如果用户没有选择“完成”,则将活动费用累加到总和
if (activityLoopResult != 6) {
sum += activityLoopResult;
}
System.out.println(); // 打印空行以改善输出格式
}
System.out.println("------------------------------"); // 分隔不同客人的信息
}
// 显示最终的总费用和平均费用
System.out.println("The total cost estimate: " + String.format("%.2f", sum) + "$");
// 计算平均每晚每人费用
double averageNightlyCostPerPerson = sum / guests / GuestNights;
System.out.println("The average nightly cost per person is: " + String.format("%.2f", averageNightlyCostPerPerson) + "$");
}
// 其他辅助方法保持不变,但需确保其逻辑正确且参数匹配
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(); // 更改变量名为 choice 以避免与参数 nights 混淆
switch (choice) { // 使用 switch 语句提高可读性
case 1:
return 100.00 * nights;
case 2:
return 200.00 * nights;
case 3:
return 300.00 * nights;
default:
System.out.println("Invalid room choice. Defaulting to Standard Room.");
return 100.00 * nights; // 或者抛出异常,或返回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(); // 更改变量名为 choice
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:
System.out.println("Invalid meal plan choice. Defaulting to No meal plan.");
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("Please enter your choice: ");
int choice = input.nextInt(); // 更改变量名为 choice
switch (choice) {
case 1:
return 300.00; // 返回固定费用,而不是 choice * 费用
case 2:
return 100.00;
case 3:
return 125.00;
case 4:
return 500.00;
case 5:
return 0;
case 6:
return 6; // 返回 6 作为退出标志
default:
System.out.println("Invalid activity choice. Defaulting to Beach Sitting.");
return 0;
}
}
}3.2 辅助方法的调整与注意事项
虽然 calculateRoomCost, calculateMealPlan, getActivitiesCost 的基本结构保持不变,但为了代码的健壮性和准确性,我们对其进行了细微调整:
- 参数使用: 确保 calculateRoomCost 和 calculateMealPlan 内部正确使用传入的 nights 参数来计算总费用(例如,100.00 * nights)。
- 返回值逻辑: getActivitiesCost 方法应返回所选活动的实际费用,而不是 choice * 费用。当用户选择“That's all--Done!”时,它应返回一个特殊值(例如 6)来指示主循环退出。
- 错误处理: 增加了 switch 语句中的 default 分支,以处理无效的用户输入,提供更友好的提示。
- 变量命名: 将方法内部的 nights 或 x 等变量名统一改为 choice,以避免与方法参数混淆,并提高可读性。
4. 示例运行与输出
运行上述代码,当输入客人数量为 2,入住晚数为 2 时,预期的输出如下:
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! Please 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! Please 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! Please 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! Please enter your choice: 6 ------------------------------ The total cost estimate: 1500.00$ The average nightly cost per person is: 375.00$
费用计算明细:
-
客人 #1:
- 房间 (标准, 2晚): $100 * 2 = $200
- 餐饮 (3餐, 2晚): $150 * 2 = $300
- 活动 (Scuba): $300
- 小计: $200 + $300 + $300 = $800
-
客人 #2:
- 房间 (豪华带景观, 2晚): $200 * 2 = $400
- 餐饮 (3餐, 2晚): $150 * 2 = $300
- 活动 (Beach Sitting): $0
- 小计: $400 + $300 + $0 = $700
- 总费用: $800 + $700 = $1500
- 平均每晚每人费用: $1500 / 2 (客人) / 2 (晚) = $375
5. 总结与最佳实践
通过本次教程,我们学习了在Java中构建交互式、多用户数据处理系统的关键技术:
- 循环结构的应用: 使用 for 循环有效处理重复的用户输入场景(如多位客人)。
- 方法参数传递: 确保方法调用时传入必要的参数,以便方法能够执行正确的计算。
- 数据累加与汇总: 使用一个累加变量(如 sum)在整个程序流程中收集和汇总来自不同方法的计算结果。
- 条件逻辑处理: 在处理特殊返回值(如 getActivitiesCost 中的退出标志 6)时,使用条件语句 (if) 避免错误累加。
- 代码可读性与健壮性: 良好的变量命名、使用 switch 语句替代冗长的 if-else if 链,以及增加默认处理逻辑,都能显著提升代码质量。
这些原则不仅适用于度假村费用计算,也适用于任何需要从用户收集多组数据、进行模块化处理并最终汇总结果的应用程序。










