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

Java调用findFutureAge方法报错‘cannot resolve symbol’的解决求助

问题解决:"cannot resolve symbol findfutureAge" 错误修复

错误原因及修复点

  • 大小写拼写不匹配:Java是大小写敏感语言,你调用的findfutureAge和定义的方法名findFutureAge不一致(注意F为大写),必须严格对应。
  • 缺少方法参数:调用方法时未传入所需的age和futureYear参数,该方法需要这两个参数才能完成计算。

修复后的完整代码

import java.util.Scanner;

public class Problem1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your name: ");
        String name = input.nextLine();
        System.out.println("Please enter your age: ");
        int age = input.nextInt();
        System.out.println("Please enter a future year: ");
        int futureYear = input.nextInt();

        // 修复:正确拼写方法名并传入参数
        System.out.println("Your age would be " + findFutureAge(age, futureYear));
    }

    public static int findFutureAge(int age, int futureYear) {
        return futureYear - 2021 + age;
    }
}

额外优化建议

  • 避免硬编码当前年份(如示例中的2021),可以使用Java 8+的java.time.Year类动态获取当前年份,让程序无需修改即可每年正常运行:
    import java.time.Year;
    // ...
    public static int findFutureAge(int age, int futureYear) {
        return futureYear - Year.now().getValue() + age;
    }
    
  • 可以在输出中加入用户姓名,让结果更友好:
    System.out.println(name + ", your age would be " + findFutureAge(age, futureYear));
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:49:57