Wednesday, August 27, 2008

Coding FIZZBUZZ using Eclipse

Tasks
The tasks was to coding a simple java program called "FIZZBUZZ" which print out all of the numbers from 1 to 100, one per line, except that when the number is a multiple of 3, print "Fizz", when a multiple of 5, print "Buzz", and when a multiple of both 3 and 5, print "FizzBuzz". From the moment I opened the eclipse program, It took 6minutes to get a results that looks correct.

Source

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%3)==0){
        System.out.println("FIZZ");
      }
      else if ((i%5)==0){
        System.out.println("BUZZ");
      }
      else{
        System.out.println(i);
      }
    }
  }
}



Results


1
2
FIZZ
4
BUZZ
FIZZ
7
8
FIZZ
BUZZ
11
FIZZ
13
14
FIZZBUZZ
16
17
FIZZ
19
BUZZ
FIZZ
22
23
FIZZ
BUZZ
26
FIZZ
28
29
FIZZBUZZ
31
32
FIZZ
34
BUZZ
FIZZ
37
38
FIZZ
BUZZ
41
FIZZ
43
44
FIZZBUZZ
46
47
FIZZ
49
BUZZ
FIZZ
52
53
FIZZ
BUZZ
56
FIZZ
58
59
FIZZBUZZ
61
62
FIZZ
64
BUZZ
FIZZ
67
68
FIZZ
BUZZ
71
FIZZ
73
74
FIZZBUZZ
76
77
FIZZ
79
BUZZ
FIZZ
82
83
FIZZ
BUZZ
86
FIZZ
88
89
FIZZBUZZ
91
92
FIZZ
94
BUZZ
FIZZ
97
98
FIZZ
BUZZ

Problems


I thought I can write this program very easily. After I opened Eclipse, however, I realized that I forgot how to start a java program. I tried to open some previous java projects that I wrote for other projects and finally figured out how to start a java program. Except that, I remembered how to implement a loop for counting 1 through 100 and other things too.