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
相关产品推荐
相关产品推荐

