FizzBuzz in Eclipse
The following article provides insight into the FizzBuzz problem presented to prospective computer programmers:
You Can't Teach Height - Measuring Programmer Competence via 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 that are multiples of both three and five print “FizzBuzz”.
In retrospect, I did not expect to encounter any problems when implementing the FizzBuzz application in Eclipse. Having some experience with Eclipse over the years and a copy of my in-class solution to this problem, I was confident the application would produce the correct output on the first run.
Expected output for the first 15 numbers:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Actual output from my implementation for the first 15 numbers:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
Obviously this wasn’t correct.
Without really thinking too much about the logic of the program, my checking conditions produced erroneous output as a result of the order I checked the value of number. By checking for numbers divisible by both 3 and 5 after checking for the values of 3 and 5, the application would print “Fuzz” for the values of 15, 30, etc. instead of “FizzBuzz”. By simply checking for the remainder of number and 15 as the first condition fixed this problem.
It took me 05:05.5 to complete the application with correct output, and 12:04.0 to finish writing in my comments.
Your program and comments don't look right.
ReplyDeleteYou're right Joe, thanks for catching that. Seems like I forgot to fix my code when changing the order in which I check the value of number.
ReplyDelete