FizzBuzz

just a general question. For some reason on the fizz buzz project when I created the class simple
class FizzBuzz {}

everything ran without error. However in the past anytime i created a class I started it
public class FizzBuzz{}

however in this project it throws out an error when I do that. Any idea why that would happen?

What kind of error (specifically), what does the rest of your code look like?

In java the file has to be named after your main class, so you should keep that in mind as well.

got it to work now. One more question. Here is the code I ran and everything prints correctly with the exception of “FizzBuzz”. Any reason why its not recognizing that part of the code? Thanks!

public class FizzBuzz {

public static void main(String args) {

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

if( i % 5 = 0 && i % 3 = 0){

System.out.println(“FizzBuzz”);

}else if(i%5 = 0){

System.out.println(“Buzz”);

}else if(i%3 = 0){

System.out.println(“Fizz”);

}else{

System.out.println(i);

}}}}

For equality condition you need == (this applies all over your code).

You can confirm and get more information in the oracle documentation https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

It’s not a problem now but in the future remember to format your code with the </> as it helps to highlight any issues in the code and it’s more readable.

Also in java, when using && or || you want to put the conditions within parentheses as they are separated. By documentation this is considered good practice. (I don’t think it’s the same in python, for contrast).

if ((x > 5) && (y <4)) {
  //some code
}
4 Likes