【问题标题】:Java int return stringJava int 返回字符串
【发布时间】:2011-09-17 06:16:32
【问题描述】:

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

private int age;

public String getAge() {

}

【问题讨论】:

  • 5 位代表说你因为称呼别人老而被否决:)
  • @user - 提示:您可以使用条件语句if 来实现它。
  • 如果你是 17、19、29 或 31 岁会怎样?
  • 十八岁的你想被当作成年人对待,所以他们也不喜欢“年轻”。 ;)
  • 嗯,没有反对意见。我会在六年后回来。

标签: java string int


【解决方案1】:

字面意思:

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";
}

【讨论】:

    【解决方案2】:

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

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

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

    注意一个公共方法会暴露给其他类,公共方法的命名应该是有意义的,而不是混淆是非常重要的。当您编写 OO 代码时,这是一个很好的做法

    【讨论】:

      【解决方案3】:

      对于只有 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();
      }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 因为我既不是 18 也不是 30,我现在的年龄显然是“??”。 O_o
        • 如果您从 5 岁开始填写“餐垫”。 - 110 岁,应该很明显为什么这种方法不是最优的。
        • breakreturn 之后?这应该是编译错误:无法访问的代码(假设"??"后面有一个;
        • @Andrew:我明白了你想让我理解的内容。但是阅读那个家伙提出的问题,他不想处理范围!如果有范围,那么 if/else 显然比 switch 好。
        猜你喜欢
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 2019-09-15
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 2011-07-09
        相关资源
        最近更新 更多