Monday, August 31, 2009

FizzBuzz, Know your drinking games...

I was a bit surprised when "FizzBuzz", a drinking game that I remembered playing a few years back was our first programming assignment for our Software Engineering class. It has been almost 2 years since I've programmed in Java and it took me a while to remember the syntax.

After installing the Eclipse IDE and quickly going over some of the samples codes, I began to write my FizzBuzz program. It took me around 15 minutes to get the program working properly, a bit longer than I've expected. The output was coming out wrong because I was using all "if" statements at first rather than using "else if".

Running into problems with such a simple code tells me that I definitely need to review and refresh my java.


Below is my code for FizzBuzz


PUBLIC class FizzBuzz {

  
PUBLIC static void main(String[] args) {

      
FOR (INT i = 1; i <= 100; i++) {

          
IF (i % 15 == 0) {
               System.out.println
("FizzBuzz";
          
}

          
ELSE IF (i % 5 == 0) {
               System.out.println
("Buzz";
          
}

          
ELSE IF (i % 3 == 0) {
               System.out.println
("Fizz";

          
}
          
          
ELSE {
               System.out.println
(i);
          
}
       }
   }

}