每日一练49——Java switch it up(8kyu)

题目

如果提供0-9之间的数字,请以文字形式返回。

输入: 1

输出:“一”。

尝试使用Switch语句。

这个卡塔是初学者用的。

测试用例:

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Test
    public void basicTests() {
        assertEquals("One", Kata.switchItUp(1));
        assertEquals("Three", Kata.switchItUp(3));
        assertEquals("Five", Kata.switchItUp(5));
    }
}

解题

My

public class Kata
{
  public static String switchItUp(int number)
  {
    switch (number) {
      case 1:
        return "One";
      case 2:
        return "Two";
      case 3:
        return "Three";
      case 4:
        return "Four";
      case 5:
        return "Five";
      case 6:
        return "Six";
      case 7:
        return "Seven";
      case 8:
        return "Eight";
      case 9:
        return "Nine";
      case 0:
        return "Zero";
    }
    return "";
  }
}

Other

public class Kata
{
  public static String switchItUp(int number)
  {
    switch (number)
    {
      case 0: return "Zero";
      case 1: return "One";
      case 2: return "Two";
      case 3: return "Three";
      case 4: return "Four";
      case 5: return "Five";
      case 6: return "Six";
      case 7: return "Seven";
      case 8: return "Eight";
    }
    return "Nine";
  }
}

不按题目出牌,但是很聪明:

public class Kata {
  public static String switchItUp(int number) {
    return new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}[number];
  }
}

后记

我还是看到很多不按题目出牌的,都很有想法。

你可能感兴趣的:(每日一练49——Java switch it up(8kyu))