9. Fizz Buzz

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foradawn/article/details/79948733

9. Fizz Buzz

Description

Given number n. Print number from 1 to n. But:

when number is divided by 3, print "fizz".
when number is divided by 5, print "buzz".
when number is divided by both 3 and 5, print "fizz buzz".

Example

If n = 15, you should return:
[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"
]

Solution

public class Solution {
    /**
     * @param n: An integer
     * @return: A list of strings.
     */
    public List<String> fizzBuzz(int n) {
        // write your code here
        ArrayList<String> list = new ArrayList<String>();
        for(int i=1;i<=n;i++){
            if(i%3==0 && i%5==0) list.add("fizz buzz");
            else if(i%3==0) list.add("fizz");
            else if(i%5==0) list.add("buzz");
            else list.add(i+"");
        }
        return list;
    }
}

编程道场之fizz buzz,重构的量度,希望大家给点评论,谢谢!

04-10

[color=#FF0000]题目:[/color]rnWrite a program that prints the numbers from 1 to 100.rnBut for multiples of three print "Fizz" instead of thernnumber and for the multiples of five print "Buzz". Forrnnumbers which are multiples of both three and fivernprint "FizzBuzz".rnrnSample output:rnrn1rn2rnFizzrn4rnBuzzrnFizzrn7rn8rnFizzrnBuzzrn11rnFizzrn13rn14rnFizzBuzzrn16rn17rnFizzrn19rnBuzzrn... etc up to 100rnrn[color=#FF0000]我的编程实现如下:[/color]rn[code=java]rnpublic class Divisor rn rn public static final int ONE = 1;rn public static final int TWO = 2;rn public static final int THREE = 3;rn public static final int FOUR = 4;rn public static final int FIVE = 5;rn public static final int SIVE = 6;rn public static final int SEVEN = 7;rn public static final int EIGHT = 8;rn public static final int NINE = 9;rnrnrnpublic class MyMathUtils rn rn //判断能否被divisor整除rn public static boolean isDiviByNumber(int number, int divisor) rn if (number % divisor == 0) rn return true;rn rn return false;rn rn rn //判断同时能否被divisor1, divisor2整除rn public static boolean isDiviByNumber(int number, int divisor1, int divisor2) rn Boolean result = false;rn if (isDiviByNumber(number, divisor1)) rn result = isDiviByNumber(number, divisor2);rn rn return result;rn rn rnrnrnpublic class Hiker rn rn private static final int DIVISOR1 = Divisor.THREE;rn private static final int DIVISOR2 = Divisor.FIVE;rn private static final String FIZZ = "Fizz";rn private static final String BUZZ = "Buzz";rn private static final String FIZZBUZZ = "FizzBuzz";rn rn public static void printNumber() rn for (int i = 1; i <= 100; i++) rn String result = getPrintResult(i, DIVISOR1, DIVISOR2);rn System.out.println(result);rn rn rnrn private static String getPrintResult(int i, int divisor1, int divisor2) rn String result = FIZZ;rn if (MyMathUtils.isDiviByNumber(i, DIVISOR1, DIVISOR2)) rn result = FIZZBUZZ;rn else if (MyMathUtils.isDiviByNumber(i, DIVISOR1)) rn result = FIZZ;rn else if (MyMathUtils.isDiviByNumber(i, DIVISOR2)) rn result = BUZZ;rn else rn result = i + "";rn rn return result;rn rnrnrnrnpublic class HikerTest rn rn @Testrn public void life_the_universe_and_everything() rn Hiker.printNumber();rn rnrn[/code]rnrn不知道这样子重构是否有缺陷,希望大家评论一下,从哪方面评论都可以,谢谢!

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试