Java int 返回字符串

发布于 2024-12-05 02:42:48 字数 238 浏览 0 评论 0原文

我有一个 int 变量,我想创建一个 String 方法来返回该 int 变量,我该怎么做?下面的示例...并将 getAge() 方法设置为在年龄为 18 时返回“young”,在年龄为 30 时返回“old”。

private int age;

public String getAge() {

}

I have an int variable and I want to create a String method to return that int variable, how do I go about it? Example below... and set the getAge() method to return "young" when age is 18, "old" when age is 30.

private int age;

public String getAge() {

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残疾 2024-12-12 02:42:48

从字面上看:

public String getAge() {
    return (30 == age)? "old":
           (18 == age)? "young":
                // because you said 18 is young, 30 is old, but didn't say
                // anything about all of the other ages!
                "I don't understand!";
}

您可以通过几种方法来做到这一点。三元结构和“if”语句通常是最好的。

// this if/else reads "(if age >= 30 then return old) else return young"
public String getAge() {
    if (30 <= age)
       return "old";
    else
       return "young";
}

// this ternary statement reads "return (if age >= 30 then old) else young"
public String getAge() {
    return (30 <= age)? "old":"young";
}

// This would be my preference
public String getAge() {
    // add bounds checking!
    if (125 <= age)
       return "You are probably dead";
    else if (0 > age)
       return "Hi doc brown! What's it like to travel through time?";
    else if (30 <= age)
       return "old";
    return "young";
}

Taken literally:

public String getAge() {
    return (30 == age)? "old":
           (18 == age)? "young":
                // because you said 18 is young, 30 is old, but didn't say
                // anything about all of the other ages!
                "I don't understand!";
}

You can do this a few ways. Ternary structure and "if" statement are generally the best.

// this if/else reads "(if age >= 30 then return old) else return young"
public String getAge() {
    if (30 <= age)
       return "old";
    else
       return "young";
}

// this ternary statement reads "return (if age >= 30 then old) else young"
public String getAge() {
    return (30 <= age)? "old":"young";
}

// This would be my preference
public String getAge() {
    // add bounds checking!
    if (125 <= age)
       return "You are probably dead";
    else if (0 > age)
       return "Hi doc brown! What's it like to travel through time?";
    else if (30 <= age)
       return "old";
    return "young";
}
盗心人 2024-12-12 02:42:48

getAge() 不是一个好的命名方法。

getAge() 将返回一个 int 数字,这让其他开发人员/用户感到困惑。

我认为你应该将你的方法命名为 getAgeClass()。

请注意,公共方法将暴露给其他类,公共方法的命名应该有意义,而不是令人困惑,这一点非常重要。当您编写 OO 代码时,这是一个很好的实践

getAge() is not a good naming method.

It confused other developers/users that getAge() will return an int number.

I think you should name your method like getAgeClass().

Notice that a public method will be exposed to other classes, it is very important your naming of public method should be meaningful, not confusing. This is a good practice when you code OO

凉墨 2024-12-12 02:42:48

对于只有 2 个年龄的小情况,我不会推荐这样做,但如果你想扩展......

你当然也可以添加显示字符串。

public enum AgeMonikers
{
    AweCute(2),
    DontTouchThat(4),
    Child(10),
    Preteen(13),
    Trouble(20),
    MoveOut(24),
    ThinkYouKnowEverythingDev(25),
    ActuallyKnowSomeDev(30),
    OldFart(100),
    WishIWasDead(Integer.MAX_VALUE);

    private int maxAge;

    private AgeMonikers(int ageLimit)
    {
        maxAge = ageLimit;
    }

    static public AgeMonikers getMoniker(int age)
    {
        if (age < 0) 
            return null;

        for(int i=values().length-1; i>0; i--)
        {
            AgeMonikers val = values()[i];

            if (age >= val.maxAge)
                return values()[i+1];
        }
        return AweCute;  // age < 2 - I know it will include negatives.
    }
}


public String getAge() 
{
    return AgeMoniker.getMoniker(age).toString();
}

I would not recommend this for the trivial case of just 2 ages, but if you want to expand...

You can of course also add display strings as well.

public enum AgeMonikers
{
    AweCute(2),
    DontTouchThat(4),
    Child(10),
    Preteen(13),
    Trouble(20),
    MoveOut(24),
    ThinkYouKnowEverythingDev(25),
    ActuallyKnowSomeDev(30),
    OldFart(100),
    WishIWasDead(Integer.MAX_VALUE);

    private int maxAge;

    private AgeMonikers(int ageLimit)
    {
        maxAge = ageLimit;
    }

    static public AgeMonikers getMoniker(int age)
    {
        if (age < 0) 
            return null;

        for(int i=values().length-1; i>0; i--)
        {
            AgeMonikers val = values()[i];

            if (age >= val.maxAge)
                return values()[i+1];
        }
        return AweCute;  // age < 2 - I know it will include negatives.
    }
}


public String getAge() 
{
    return AgeMoniker.getMoniker(age).toString();
}
半透明的墙 2024-12-12 02:42:48

最好使用 switch 语句,因为条件表达式不会改变。
所以这样做:

switch (age) {
    case 18:
        return "young";
    case 30:
        return "old";
    default:
        return "??";
    }

Better use a switch statement since the conditonal expression is not changing.
So do it like:

switch (age) {
    case 18:
        return "young";
    case 30:
        return "old";
    default:
        return "??";
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文