FizzBuzz challenge (im stuck pls help)

hi everyone, i just wanted to ask for some help with the FizzBuzz exercise, im really at a loss with it,

any comments or explanations would really help
https://www.codecademy.com/courses/learn-java/projects/java-fizzbuzz
my code is below:

public static void main(String args){

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

if else (i / 3 == 0 && i / 5 == 0) { console.log(“FizzBuzz”)

}

if else ( i / 3 == 0 ) { console.log(“Fizz”);

}

if else ((i / 5) == 0){ console.log(“Buzz”);

}

else { console.log(i); } }

thanks, jack

Hello. @jackburton0958773581, and welcome to the forums.

I’m not sure how you came to this challenge, but you’re missing some rudimentary understanding of syntax. I’m not aware of a language where if else is valid syntax. You also have a mixture of Java and JavaScript which are two completely different languages.

I would first decide which language you’re coding in, and then study the basics before jumping into a challenge like FizzBuzz.

1 Like

hi midlindner, thanks for your reply!! yes i got a bit stuck so i started looking online for solutions and must of been reading the same FizzBuzz but in another language lol and then trying to copy parts of it.

anyhow! i just re corrrected all of tht n kept using javac FizzBuzz.java found all errors, “if”, “else”, took wrong language out and added %, all GOOD complete :slight_smile:

thanks for ur input!! have a gd day

codeacademy says i can move on to next task, but i noticed any multiples of 3 are not being replaced by Fizz , instead of it replacing it, it just inserts Fizz then after if the multiple of 3 still appears, im not sure this is right?

heres my code:

public static void main(String args){

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

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

}

if ( i % 3 == 0 ) { System.out.println(“Fizz”);

}

if (i % 5 == 0){ System.out.println(“Buzz”);

}

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

}

}

https://www.codecademy.com/courses/learn-java/projects/java-fizzbuzz

It can often be helpful to go step-by-step through your code on the parts that aren’t working.

For example, let’s take 3 (the third iteration of the loop). 3 does not satisfy the condition of the first if statement, since 3 % 5 is not equal to 0. Then, 3 satisfies the condition of the next if statement, since 3 % 3 is equal to 0. "Fizz" is printed.

Then, 3 does not satisfy the condition of the next if statement, since 3 % 5 is not equal to 0. Since this if statement is an if-else statement, we execute the code inside the else block since the boolean condition was false. This means we print the number, 3.

It is not only the multiples of 3 that cause problems. There is also a different issue in that any number that is a multiple of both 3 and 5 will result in "FizzBuzz", "Fizz", "Buzz", and the number itself all being printed (try going step-by-step as I did above with the number 15, for example).

What changes could you make to your code to ensure only one string or number is printed for each iteration?

I strongly recommend you study the basics first before continuing with this project, or to at least review the Conditionals and Control Flow lesson.

thanks victoria i will re visit the conditionals and control flow lesson to get more familiar

kind regards :slight_smile:

1 Like

You can find all the Java cheatsheets here (it isn’t necessary to pass the lessons to access them). However, it looks like the cheatsheet for this specific unit is currently unavailable.

Side note: I would advise against posting your email on a public forum.

no worries dr_victoria , thanks im just reading a re typing notes out rgrds conditionals and control flow, probably better to digest info

ive got it working now :slight_smile: thanks for your assistance, appreciate it!!!

public static void main(String args){

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

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

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

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

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

}

}

1 Like