Fizz Buzz issue

Hi all!

I am doing the following exercise: Learn Java | Codecademy | FizzBuzz

The output for code is ALMOST right. Fizz and Buzz seem to come out on the intended numbers. But BuzzFizz won’t output the multiples of 3 and 5.

Here’s what I have tried for that else if statement:

else if(i % 15 == 0) {
System.out.println("FizzBuzz")

// other version I have tried: 
else if (i % 3 == 0 && i % 5 ==0) {
System.out.println("FizzBuzz");

Snippet of code

public class FizzBuzz {

  

  public static void main(String[] args) {

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

      if(i % 3 == 0) {

        System.out.println("Buzz");

      }

    else if(i % 5 == 0) {

      System.out.println("Fizz");

    }

    else if(i % 15 == 0) {

      System.out.println("FizzBuzz");

    }

    else {

      System.out.println(i);

  }

  }

 }

}

The moment you have a number which is divisible by 3 and 5, one of the other conditions are true (divisible by 3 or 5).

once an if or else if condition evaluates to true, the remaining else if or else clauses are not checked/executed.

1 Like

I did the “FizzBuzz” check first in my code by doing if (i % 3 == 0 && i % 5 == 0) then I did the check
for the Buzz and Fizz after that.

We lessen the logic load with the LCM…

(i % 15 == 0)

Thanks I rewrote the Fizz Buzz program with the new logic of the (i % 15 == 0)