转载:Java developer group的一个面试题

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 121 收藏 0 点赞 0 评论 0

题目:Write a program that prints the numbers from 1 to 100. But for multiples of three print ‘Fuzz’ instead of the number and for the multiples of five print ‘Bizz’ For numbers which are multiples of both three and five print ‘FuzzBizz’.

Let’s see how your code looks like:

METHOD 1 (basically the same as Abhishek’s method)
——————————————————————————–

for (String s = null, int n = 1; n <= 100; n++) {

s = (n%15 == 0) ? “FuzzBizz” : (n%3 == 0) ? “Fuzz” : (n%5 == 0) ? “Bizz” : Integer.toString(n);

System.out.println(s);
}

METHOD 2 (my favourite):
————————————–

for (String s = null, int n = 1; n <= 100; n++) {

s = (n%3 != 0 && n%5 != 0) ? Integer.toString(n) : (n%5 != 0) ? “Fuzz” : (n%3 != 0) ? “Bizz” : “FuzzBizz”;

System.out.println(s);
}

METHOD 3: Somay’s method

METHOD 4:
——————

private static String[] s = new String[15];

static {
s[0] = “FuzzBizz”;
s[3] = s[6] = s[9] = s[12] = “Fuzz”;
s[5] = s[10] = “Bizz”;
}

public static void main(String[] args) {
for(int i = 1; i <= 100; i++) {

if (s[i%15] != null) {

System.out.println(s[i%15]);

} else {

System.out.println(i);

}

}
}

评论列表
文章目录