# FizzBuzz

Hi i tried to solve the FizzBuzz project but honestly i’m not that good at math and logic, I’ve working on that because i really want to learn to Code.

But i need to understand what im doing or what the code is saying instead of just coding like nothing.

this is my code

class FizzBuzz {

public static void main(String args) {

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

System.out.println(i);

}

if (i % 3 == 0) {
System.out.println(“Fizz”);
}
else if (i % 5 == 0) {
System.out.println(“Buzz”);
}
else if (i % 3 == 0 && i % 5 == 0) {
System.out.println(“FizzBuzz”);

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

}
}

It looks like you’re close! Just a couple of things to note. You want all of the code that’ll output something (the number, or Fizz/Buzz, etc) to be inside the loop, since you want it to happen for every number in the range. Right now, you’ve only got this line System.out.println(i); in the loop.

Next, you might want to have a look at the order of your code. Assuming everything’s inside the loop, currently, you code will run (for each number) in the following order:

1. Print the number. This will always happen, but remember, you only want it to print for numbers that aren’t mutiples of three or five.
2. It’ll check if the number’s divisible by 3. If it is, then “Fizz” will be printed.
3. If not, it’ll check if the number’s divisible by 5. If so, then it’ll print “Buzz”.
4. Lastly, it’ll check to see if the number is divisible by both 3 and 5. But the problem is here: since the code above has already checked for divisibility by 3 (the first conditional), it’ll never reach the second else if block.
Further explanation of point 4

Consider the number 15. It is divisible by both 3 and 5, so it should print FizzBuzz:
the first check in if (i % 3 == 0), so in our case,if (15 % 3 == 0) . Since 15 is divisible by 3, this will return true, and "Fizz" will be printed. But since the rest of the logic is in else if blocks, if the first block runs (which it does with 15) none of the other blocks will run, and you won’t get FizzBuzz printed.

I hope this helps!

1 Like

Thank you so Much, this helped me a Lot to understand what i’m doing.

1 Like

No problem, and happy coding!