Monday, August 27, 2007

04.FizzBuzz

It took me about 1 hour to complete this assignment.

Firstly, I closed the "Welcome" page and clicked on File>New>Java Project, then I entered a project name, then I clicked Next, then clicked Finish.

Secondly, I opened File > New > File, then I selected a parent folder and entered a file name.

Thirdly, I started to write "FizzBuzz" program. After I wrote the program, saved it and run it, a launch error was appearred and said "Selection does not contain a main type". After spending a while to think what went wrong and why I had this error.

I decided to exit the eclipse platform. Then, I re-opened it again. I decided to read the contents of "Welcome" page and I found a "Tutorial" on the "Welcome" page. After I opened the "Tutorial", I learned how to create a Hello World application.

For my project, I basically followed the steps of Hello World application and wrote a "for" loop to run each number from 1 to 100. Inside the "for" loop, I have if-else if-else to check each number whether is multiple of both 3 and 5, or 3, or 5.

The following is my "04.FizzBuzz" program.



public class FizzBuzz {

public static void main(String[] args) {

for (int number = 1; number <= 100; number++){

if ((number % 3 ==0) && (number % 5 == 0)){
System.out.println("FizzBuzz");
}//end if

else if (number % 3 == 0){
System.out.println("Fizz");
}//end else if

else if (number % 5 == 0){
System.out.println("Buzz");
}//end else if

else{
System.out.println(number);
}//end else

}//end for

}//end main

}//end class FizzBuzz



No comments: