I have solved this all inside the main method, however the professor has asked we follow very specific guidelines. Here are the instructions:
Create methods with the following signatures (don’t forget
static
):
private static boolean isFizz (int val)
check if a multiple of 3.private static boolean isBuzz(int val)
check if a multiple of 5.Create a class member variable (this means it’s not inside a method, but is inside a class):
private static int counter
- Note: you can initialize this when you declare it, or inside the main method.
- In the main method:
- Use the counter to iterate from 1 to 100.
- Use the two other methods you define to determine what to print.
- Note that the methods should not print anything, they just return a boolean value.
- Your program should include at least one of each of the following:
- branch control statement (like
if
).- loop.
public class Fizzy {
//checking if a multiple of 3
private static boolean isFizz(int i){
if (i % 3 == 0){
return true;
}
return false;
}
//checking if a multiple of 5
private static boolean isFuzz(int i){
if (i % 5 == 0){
return true;
}
return false;
}
//professor wants a class here outside of main with a private static int.
//But I get an error and I'm not sure what I need to do to fix it.
//also, is this where my booleans need to be called?
public class Counter {
private static int counter(int x);
}
public static void main (String [] args){
//I think I'm supposed to call something here?
//I've tried Counter a = new Counter(); but it doesn't like it.
//I've tried new booleans but also doesn't like it.
/**
* for loop to iterate i to 100
*/
//counter is supposed to be iterated here. However I am not sure
//how to exactly access counter from a separate class.
for(counter; counter <= 100; ++counter){
//if Statement to check if a multiple of 3 and 5.
if (counter % 3 == 0 && counter % 5 == 0){
System.out.println("FizzBuzz");
}
// else if statement to check if multiple of 3
else if (isFizz == true){
System.out.println("Fizz");
}
//else if statement to check if multiple of 5
else if (isFuzz == true){
System.out.println("Buzz");
}
//else just run the loop
else {
System.out.println(counter);
}
}
}
}
}
It is supposed to go like this:
1
2
Fizz
4
Buzz
Fizz
.
.
.
14
FizzBuzz
16
And so on.