patternjavaMajor
Yet another boring FizzBuzz
Viewed 0 times
yetanotherfizzbuzzboring
Problem
I've just finished my first program written in Java. It's a simple, boring, yet well-known FizzBuzz challenge where you have to:
Write a program that prints the numbers from 1 to 100. But for
multiples of three print “Fizz” instead of the number and for the
multiples of five print “Buzz”. For numbers which are multiples of
both three and five print “FizzBuzz”."
I know there are some other FizzBuzz questions here, but I'd really like to start learning Java in the right way, which means that I'm looking for:
Coming from Python world, I was tempted to create a separate method named
Write a program that prints the numbers from 1 to 100. But for
multiples of three print “Fizz” instead of the number and for the
multiples of five print “Buzz”. For numbers which are multiples of
both three and five print “FizzBuzz”."
I know there are some other FizzBuzz questions here, but I'd really like to start learning Java in the right way, which means that I'm looking for:
- Naming conventions
- Coding standards and best practices
Coming from Python world, I was tempted to create a separate method named
fizzBuzz() which I then called in my main() method. Is this a bit overkill for such a simple task?package fizzbuzz;
class FizzBuzz {
/*
Write a program that prints the numbers from 1 to 100. But for multiples of
three print “Fizz” instead of the number and for the multiples of five print
“Buzz”. For numbers which are multiples of both three and five print
“FizzBuzz”."
*/
public static void fizzBuzz() {
for(int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0)
System.out.println("FizzBuzz");
else if (i % 3 == 0)
System.out.println("Fizz");
else if (i % 5 == 0)
System.out.println("Buzz");
else
System.out.println(i);
}
}
public static void main(String[] args) {
fizzBuzz();
}
}Solution
Coming from Python world, I was tempted to create a separate method named
Well, it is a little bit too much for such a simple task, but it is a good habit to do so and if I were you I would stick to it.
Overall your
but
This is because you omitted braces
Omitting the braces can lead to hidden, and therefore hard to find bugs. I would like to encourage you to always use them.
Another thing is that you have some magic numbers, like
You could add a
fizzBuzz() which I then called in my main() method. Is this a bit of overkill for such a simple task ? Well, it is a little bit too much for such a simple task, but it is a good habit to do so and if I were you I would stick to it.
Overall your
fizzBuzz looks good. - Naming -> Check
but
- Best Practices -> Fail
This is because you omitted braces
{} although they are optional for single instruction if, else if and else branches. Omitting the braces can lead to hidden, and therefore hard to find bugs. I would like to encourage you to always use them.
Another thing is that you have some magic numbers, like
1, 100, 3, and 5 in your code which should either be extracted to some meaningful named constants, or passed to the method as parameters. You could add a
isDivisibleBy(int value, int divider) method as well to make the responsibilities clear.Context
StackExchange Code Review Q#151620, answer score: 30
Revisions (0)
No revisions yet.