You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java健身会员程序问题:custMonths返回0及主方法对象调用疑问

解决你的Java健身会员程序问题

嘿,我来帮你搞定这两个头疼的问题!咱们一个一个来拆解:

问题1:custMonths 始终返回0

这种情况大概率是变量赋值环节出了问题,常见的原因有这几种:

  • 你只初始化了int custMonths = 0;,但没有把用户输入的值赋给它(比如漏写了custMonths = scanner.nextInt();)
  • 读取输入后,又不小心把变量重新赋值为0了
  • 输入读取的顺序不对,比如先读年龄后读月数,但代码里搞反了,导致月数没被正确读取

举个正确的输入读取示例,顺便加上需求里的「输入大于0」验证:

Scanner scanner = new Scanner(System.in);
int custMonths = 0;

System.out.println("请输入会员月数:");
// 循环验证输入必须大于0
do {
    if (scanner.hasNextInt()) {
        custMonths = scanner.nextInt();
        if (custMonths <= 0) {
            System.out.println("月数必须大于0,请重新输入:");
        }
    } else {
        System.out.println("请输入有效的数字:");
        scanner.next(); // 清除无效输入
    }
} while (custMonths <= 0);

问题2:子方法中使用主方法的对象

主方法是static的,而你在主方法里创建的对象如果是类的实例,要在子方法里用它,有两种常用方式:

方式1:把对象作为参数传递给子方法

如果子方法是static的,直接把主方法里的对象(或者对象的属性)传进去就行:

public static void main(String[] args) {
    // 主方法里获取验证后的输入
    Scanner scanner = new Scanner(System.in);
    int custAge = getValidInput("请输入客户年龄:");
    int custMonths = getValidInput("请输入会员月数:");
    
    // 把参数传给子方法
    printMemberFees(custAge, custMonths);
}

// 封装输入验证的子方法
private static int getValidInput(String prompt) {
    Scanner scanner = new Scanner(System.in);
    int input = 0;
    System.out.println(prompt);
    do {
        if (scanner.hasNextInt()) {
            input = scanner.nextInt();
            if (input <= 0) {
                System.out.println("输入必须大于0,请重新输入:");
            }
        } else {
            System.out.println("请输入有效的数字:");
            scanner.next();
        }
    } while (input <= 0);
    return input;
}

// 计算并打印费用的子方法
private static void printMemberFees(int age, int months) {
    double monthlyRate = calculateMonthlyRate(age);
    double totalCost = monthlyRate * months;
    System.out.printf("客户月费率:$%.2f,总费用:$%.2f%n", monthlyRate, totalCost);
}

// 计算月费率的子方法
private static double calculateMonthlyRate(int age) {
    if (age < 18) {
        return 29.99;
    } else if (age >= 65) {
        return 39.99;
    } else {
        return 49.99;
    }
}

方式2:创建类的实例,通过实例调用非静态子方法

如果你的子方法是非静态的(属于类的实例),那在主方法里创建类的实例后,就可以直接调用子方法,实例里的属性会被自动使用:

public class FitnessMemberProgram {
    private int custAge;
    private int custMonths;

    // 构造方法初始化属性
    public FitnessMemberProgram(int age, int months) {
        this.custAge = age;
        this.custMonths = months;
    }

    // 非静态子方法:计算月费率
    private double calculateMonthlyRate() {
        if (this.custAge < 18) {
            return 29.99;
        } else if (this.custAge >= 65) {
            return 39.99;
        } else {
            return 49.99;
        }
    }

    // 非静态子方法:打印费用
    public void printFees() {
        double monthlyRate = calculateMonthlyRate();
        double totalCost = monthlyRate * this.custMonths;
        System.out.printf("客户月费率:$%.2f,总费用:$%.2f%n", monthlyRate, totalCost);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 获取验证后的输入
        int age = getValidInput("请输入客户年龄:");
        int months = getValidInput("请输入会员月数:");
        
        // 创建类的实例
        FitnessMemberProgram member = new FitnessMemberProgram(age, months);
        // 调用非静态子方法
        member.printFees();
    }

    // 静态工具方法:处理输入验证
    private static int getValidInput(String prompt) {
        Scanner scanner = new Scanner(System.in);
        int input = 0;
        System.out.println(prompt);
        do {
            if (scanner.hasNextInt()) {
                input = scanner.nextInt();
                if (input <= 0) {
                    System.out.println("输入必须大于0,请重新输入:");
                }
            } else {
                System.out.println("请输入有效的数字:");
                scanner.next();
            }
        } while (input <= 0);
        return input;
    }
}

这样既解决了你的两个问题,也满足了需求里的输入提示、有效信息输出和输入验证的要求~

内容的提问来源于stack exchange,提问作者SamAndrew81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:36:54