
在开发交互式应用程序时,经常需要从用户那里收集多组数据,并对这些数据进行处理和汇总。本教程将以一个度假村费用计算系统为例,演示如何在java中有效地处理多位客人的输入、利用方法(methods)进行模块化计算,并将这些计算结果准确地累加和显示给用户。我们将重点解决如何为每位客人循环收集信息,以及如何从各个计算方法中获取并汇总费用。
原始代码面临的主要挑战是:
针对这些问题,我们将采用以下解决方案:
该系统由一个主方法 main 和三个辅助计算方法组成:
所有方法都使用一个全局的 Scanner 对象 input 来获取用户输入。
立即学习“Java免费学习笔记(深入)”;
main 方法是整个程序的入口和控制中心。为了实现多客人数据处理和费用汇总,我们需要对其进行以下关键修改:
以下是优化后的 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;
}
}
}虽然 calculateRoomCost, calculateMealPlan, getActivitiesCost 的基本结构保持不变,但为了代码的健壮性和准确性,我们对其进行了细微调整:
运行上述代码,当输入客人数量为 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$
费用计算明细:
通过本次教程,我们学习了在Java中构建交互式、多用户数据处理系统的关键技术:
这些原则不仅适用于度假村费用计算,也适用于任何需要从用户收集多组数据、进行模块化处理并最终汇总结果的应用程序。
以上就是Java 度假村费用计算与多用户数据汇总教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号