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

Java中return关键字的正确使用及Person类growOlder方法修正问题

Fixing Your Person Class's growOlder Method

Let's walk through the issues in your code and fix them to match your requirements—you're almost there!

Key Problems in the Current growOlder Method

  • Wrong return type: The method is declared to return int, but your requirement specifies it should return a boolean (true when age is updated, false otherwise). This is why you were forced to return nYears (to avoid compilation errors), which caused the extra numbers in your output.
  • Hardcoded age increment: You're adding 13 directly instead of using the passed nYears parameter. The requirement says to increase age by the value of nYears (when it's non-negative).
  • Incorrect output handling: You're printing true/false inside the method, but the requirement expects the method to return these values so the caller can handle printing (which is why your output has extra digits from the returned nYears).

Corrected Full Person Class

public class Person{
    private String name;
    private int age;

    // Removed unused `getOlder` field since it wasn't being used
    public Person(){
        this("Otto", 25);
    }
    public Person( String name, int age ){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }

    // Fixed growOlder method
    public boolean growOlder(int nYears){
        if(nYears >= 0){
            age += nYears; // Use the passed parameter instead of hardcoding 13
            return true;   // Return true instead of printing it
        } else {
            return false;  // Return false without modifying age
        }
    }

    public String toString( ){
        return getName() + " " + getAge();
    }
}

Explanation of Changes

  1. Return type updated: Changed public int growOlder(...) to public boolean growOlder(...) to align with the requirement of returning a true/false status.
  2. Dynamic age increment: Replaced age = age + 13 with age += nYears so the age increases by the actual value passed to the method (e.g., 13 during your test).
  3. Return boolean values directly: Removed the System.out.println calls inside the method. Instead, we return true or false—when you call this method from your test code, you can print the returned value, which will give you the clean output you expect.
  4. Cleaned up unused code: Got rid of the private int getOlder field since it wasn't serving any purpose.

Test Output Verification

If you run test code like this:

public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person); // Prints "Otto 25"
        System.out.println(person.growOlder(13)); // Prints "true", age becomes 38
        System.out.println(person); // Prints "Otto 38"
        System.out.println(person.growOlder(-13)); // Prints "false", age stays 38
        System.out.println(person); // Prints "Otto 38"
    }
}

You'll get exactly your expected output:

Otto 25
true
Otto 38
false
Otto 38

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 11:47:34