FizzBuzz

I don’t know where to post this but am having trouble with the fizz buzz project in java. Can’t get the terminal (compiler?) to show the words. I used the similar code in the Swift project.

public FizzBuzz
{
    public static void main(String[] args)
  {  
    int i = 0;
    int max = 100;
    String s = "";
    for(i = 0; i <= max; i++)
    {
      if(i % 3 = 0)
      { 
       s = "Fizz";
        System.out.println(s);
        }
      else if(i  % 5 = 0)
    {
      s = "Buzz";
      System.out.println(s);
    }
  else if (i % 3 = 0 && i % 5 = 0)

  {
    s = "FizzBuzz";
    System.out.println(s);
  }
  else
  {
    System.out.println(i);
  }
  } 

Thanks in advance.

Haven’t looked closely at the logic and correctness of the code, but here are a few things to consider.

# You wrote:
public FizzBuzz

# It should be:
public class FizzBuzz 
  • Also you are missing a couple of closing curly braces at the bottom of your code. I assume it is just because of copy pasting in the forums, but you may want to double check.

  • In your conditions, you have used the assignment operator = instead of the equality comparison operator ==. For Example,

# You wrote:
if (i % 3 = 0)

# It should be:
if (i % 3 == 0)
  • Will you ever reach this condition?
else if (i % 3 = 0 && i % 5 = 0)

Your else if condition is checking for multiples of 15 (i.e. multiples of both 3 and 5), but your structure is
if - else if - else. If one of the conditions is true, then the other conditions are skipped. This means that if the condition at the top i % 3 == 0 is true, then you will not reach the else if condition under discussion. You may want to think more about where this condition should be positioned.

  • Your loop begins at 0, whereas the project says the range of numbers is 1 to 100.
1 Like

Thanks for the reply. I will look into it and re check my code. Can I ask a slightly unrelated question? Is there a way to not use Bash for these projects? i get it’s to learn how to compile for java but I frankly find using it annoying. I’m working on another project using arrays and I can’t get it to recognize the size method.