Problem
This questions is originally from http://contestcoding.wordpress.com/2013/06/28/fizz-buzz-bizz-fuzz/.
- Print the integers from 1 to 100,
- but for the multiples of 3, print “Fizz” instead and
- for multiples of 5, print “Buzz”.
- If the number contains a 3 (for example 23), print “Bizz” and
- if the number contains a 5, print “Fuzz”
- (if it contains multiple 3s or 5s, just print one “Bizz” or “Fuzz”).
- If the number contains more than one of these attributes, print every word (for example 33 prints “FizzBizz”, as 33 is both a multiple
of 3 and contains the digit 3).
My Java solution is:
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i < 101; i++) {
// Set this to true when one of the special conditions is met.
boolean printed = false;
if (i % 3 == 0) {
// When i is divisible by 3, then print "Fizz"
printed = true;
System.out.print("Fizz");
} else if (i % 5 == 0) {
// When i is not divisible by 3 but is divisible by 5, then print "Buzz"
printed = true;
System.out.print("Buzz");
}
if (Integer.valueOf(i).toString().indexOf("3") != -1) {
// When i has the digit 3 in it, then print "Bizz"
printed = true;
System.out.print("Bizz");
} else if (Integer.valueOf(i).toString().indexOf("5") != -1) {
// When i has the digit 5 in it, then print "Fuzz"
printed = true;
System.out.print("Fuzz");
}
if (printed == false) {
// The number does not satisfy any of the special conditions above.
System.out.print(i);
}
System.out.println();
}
}
}
Please provide code review comments.
Solution
Overall, this is a pretty straightforward program. See the bottom of my answer for an Extreme Makeover: Code Edition of the program.
You have a hard-coded “magic number” in your for
loop. It would be better to use a variable.
for (int i = 1; i < 101; i++) // not the best
int num = 101;
for (int i = 1; i < num; i++); // better
Your if
test conditions can be shortened a bit.
if (Integer.toString(i).indexOf("3") != -1)
Your logic is a bit off. You should actually have fewer else if
conditions (this is rarely the case, but here it is applicable). For example, when i
reaches “15”, is should print “FizzBuzzFuzz”, but your program only prints “FizzFuzz”.
Final code:
public class Test
{
public static void main(String... args)
{
int num = 101;
for (int i = 1; i < num; i++)
{
boolean printed = false;
if (i % 3 == 0)
{
printed = true;
System.out.print("Fizz");
}
if (i % 5 == 0)
{
printed = true;
System.out.print("Buzz");
}
if (Integer.toString(i).indexOf("3") != -1)
{
printed = true;
System.out.print("Bizz");
}
if (Integer.toString(i).indexOf("5") != -1)
{
printed = true;
System.out.print("Fuzz");
}
if (printed == false) System.out.print(i);
System.out.println();
}
}
}
Extreme Makeover: Code Edition
Let’s use a StringBuilder
and some ternary operators. And let’s get rid of that boolean
.
public class Test
{
public static void main(String... args)
{
int num = 101;
for (int i = 1; i < num; i++)
{
StringBuilder sb = new StringBuilder();
if(i % 3 == 0) sb.append("Fizz");
if(i % 5 == 0) sb.append("Buzz");
if(Integer.toString(i).indexOf("3") != -1) sb.append("Bizz");
if(Integer.toString(i).indexOf("5") != -1) sb.append("Fuzz");
if (sb.length() == 0) System.out.print(i);
else System.out.print(sb);
System.out.println();
}
}
}
-
Your implementation is broken, for example 15 should print
FizzBuzzFuzz
but yours will printFizzFuzz
. 35 should printBuzzBizzFuzz
but yours will printBuzzBizz
. -
Turning an integer into a string can be done with
Integer.toString(i)
. -
For reusability your code should be encapsulated in a class which you can instantiate and play as many games as you like.
-
Once you have refactored the code into a class consider decoupling logic from output. For example you can pass in an
Appendable
to which you can append your output without having to care where it ends up.