每日一练115——Java莱昂纳多迪卡普里奥和奥斯卡(8kyu)

题目

你必须编写一个描述Leo的函数:

def leo(oscar):
  pass

如果奥斯卡是(整数)88,你必须返回“Leo finally won the oscar! Leo is happy”(Leo终于赢得奥斯卡!Leo很高兴。)
如果奥斯卡是86,你必须返回“Not even for Wolf of wallstreet?!(甚至连墙壁的狼都没有?!”)
如果它不是88或86(并且低于88)你应该返回“When will you give Leo an Oscar?”(你什么时候给狮子座一个奥斯卡?)
如果它超过88你应该返回“Leo got one already!”(狮子座已经有一个!)

解题

My:

public class Kata
{
  public static String leo(final int oscar)
  {
    String str = null;
    if (oscar == 88) {
      str = "Leo finally won the oscar! Leo is happy";
    } else if (oscar > 88) {
      str = "Leo got one already!";
    } else if (oscar < 88) {
      if (oscar == 86) {
        str = "Not even for Wolf of wallstreet?!";
      } else {
        str = "When will you give Leo an Oscar?";
      }
    }
    return str;
  }
}

Other:

public class Kata {

  public static String leo(final int oscar) {
    String s;
    switch (oscar) {
      case 88: s="Leo finally won the oscar! Leo is happy"; break;
      case 86: s="Not even for Wolf of wallstreet?!"; break;
      default:
        s = oscar<88 ? "When will you give Leo an Oscar?" : "Leo got one already!";
    }
    return s;
  }
}
public class Kata
{
  public static String leo(final int oscar)
  {
    if (oscar == 88) {
      return "Leo finally won the oscar! Leo is happy";
    } else if (oscar == 86) {
      return "Not even for Wolf of wallstreet?!";
    } else if (oscar < 88) {
      return "When will you give Leo an Oscar?";
    } else {
      return "Leo got one already!";
    }
  }
}
public class Kata
{
  public static String leo(final int oscar){
    return (oscar > 88) ? "Leo got one already!" : (oscar == 88) ? "Leo finally won the oscar! Leo is happy" : (oscar == 87 || oscar < 86) ? "When will you give Leo an Oscar?" : "Not even for Wolf of wallstreet?!";
  }
}

后记

这种题就是容易出现很多实现方法的变种。

你可能感兴趣的:(每日一练115——Java莱昂纳多迪卡普里奥和奥斯卡(8kyu))