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

已声明并赋值的charge和deduction1变量提示无法解析为变量如何解决?

问题根因

你遇到的变量无法解析报错核心是Java局部变量的作用域限制:

  • 你在local_l()方法内定义的deduction1、在calculation()方法内定义的charge都属于方法局部变量,仅在当前方法的花括号范围内有效,all()、old_age_pensioner()等外部方法无法直接访问这两个变量。

除此之外你的代码还有多处逻辑设计问题:

  1. calculation()方法为void返回类型,计算出的停车基础费用无法传递给调用方;local_l()虽然有返回值,但你在all()中调用时没有用变量接收返回值,结果直接被丢弃
  2. old_age_pensioner()方法的参数要求传入3个值,你调用时只传了2个,参数数量不匹配;且方法中判断是否为养老金领取者后就直接return,后续计算最终费用的代码永远不会被执行
  3. 未处理输入异常分支:比如停车时长超过8小时、用户输入非Yes/No选项、nextInt()读取整数后残留换行符导致后续nextLine()读空等问题
  4. 重复创建Scanner对象,属于冗余操作
修复后完整可运行代码
import java.util.Scanner;

class Parking {
    // 全局共用一个Scanner即可
    private static final Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] a) {
        all();
        SCANNER.close();
        System.exit(0);
    }

    // 获取本地居民抵扣额
    public static double getLocalDeduction() {
        while (true) {
            System.out.println("Do you live locally ? (Yes or No)");
            String local = SCANNER.nextLine().trim();
            if ("Yes".equals(local)) {
                return 1.0;
            } else if ("No".equals(local)) {
                return 0.0;
            } else {
                System.out.println("Invalid answer, please answer : Yes or No (exactly as written)");
            }
        }
    }

    // 计算基础停车费,残障人士直接返回0
    public static double calculateBaseCharge() {
        while (true) {
            System.out.println("Are you disabled? (Yes or No)");
            String disabled = SCANNER.nextLine().trim();
            if ("Yes".equals(disabled)) {
                System.out.println("Parking is free");
                return 0.0;
            } else if ("No".equals(disabled)) {
                System.out.println("Please enter your parking hours:");
                while (!SCANNER.hasNextInt()) {
                    System.out.println("Invalid input, please enter a number:");
                    SCANNER.next();
                }
                int hours = SCANNER.nextInt();
                // 吃掉nextInt残留的换行符
                SCANNER.nextLine();
                if (hours <= 0) {
                    System.out.println("Parking hours cannot be less than 1");
                    continue;
                }
                if (hours == 1) {
                    return 1.00;
                } else if (hours >= 2 && hours <= 4) {
                    return 4.00;
                } else if (hours >= 5 && hours <= 6) {
                    return 4.50;
                } else if (hours >= 7 && hours <= 8) {
                    return 5.50;
                } else {
                    // 超过8小时的规则你可以自行调整,这里暂时按8小时费用计算
                    return 5.50;
                }
            } else {
                System.out.println("Invalid answer, please answer : Yes or No (exactly as written)");
            }
        }
    }

    // 获取养老金领取者抵扣额
    public static double getOapDeduction() {
        while (true) {
            System.out.println("Are you an old age pensioner? (Yes or No)");
            String oap = SCANNER.nextLine().trim();
            if ("Yes".equals(oap)) {
                return 2.0;
            } else if ("No".equals(oap)) {
                return 0.0;
            } else {
                System.out.println("Invalid answer, please answer : Yes or No (exactly as written)");
            }
        }
    }

    public static void all() {
        // 接收各个方法返回的结果,再统一计算
        double baseCharge = calculateBaseCharge();
        if (baseCharge == 0) {
            // 残障免费直接结束
            return;
        }
        double localDeduction = getLocalDeduction();
        double oapDeduction = getOapDeduction();
        // 抵扣后最低费用为0,避免出现负数
        double finalCharge = Math.max(0, baseCharge - localDeduction - oapDeduction);
        System.out.printf("The parking charge is %.2f%n", finalCharge);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 06:45:06