美文网首页
Create Phone Number

Create Phone Number

作者: Magicach | 来源:发表于2017-12-29 11:25 被阅读0次

    Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.
    Example:

    Kata.createPhoneNumber(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) // => returns "(123) 456-7890"

    The returned format must be correct in order to complete this challenge.
    Don't forget the space after the closing parenthesis!
    Good Solution1:

    public class Kata {
      public static String createPhoneNumber(int[] numbers) {
        return String.format("(%d%d%d) %d%d%d-%d%d%d%d",numbers[0],numbers[1],numbers[2],numbers[3],numbers[4],
                                      numbers[5],numbers[6],numbers[7],numbers[8],numbers[9]);
      }
    }
    

    Good Solution2:

    public class Kata {
      public static String createPhoneNumber(int[] numbers) {
        return String.format("(%d%d%d) %d%d%d-%d%d%d%d", java.util.stream.IntStream.of(numbers).boxed().toArray());
      }
    }
    

    Good Solution3:

    import java.util.Arrays;
    
    public class Kata {
    
        private static String PHONE_FORMAT = "(%d%d%d) %d%d%d-%d%d%d%d";
    
        public static String createPhoneNumber(int[] numbers) {
            Integer[] numbersInt = Arrays.stream(numbers).boxed().toArray(Integer[]::new);
            return String.format(PHONE_FORMAT, numbersInt);
        }
    }
    

    相关文章

      网友评论

          本文标题:Create Phone Number

          本文链接:https://www.haomeiwen.com/subject/nirigxtx.html