Fizzbuzz counters 1-20 and check for divideables and not dividables

329 views Asked by At

So i have a problem with java that when i run the program the console prints out this

"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19Buzz" 

what i want it to do is , if its dividable with BOTH 3 and 5 then print out fizzbuz

if divideable with 3 print out fizz if dividable with 5 print out Buzz

My code looks like this :

public static void main(String[] args) {


        for(int i=1; i<=20; i++)

        {
            if (i % 3==0 && i % 5==0) // fizzbuzz
                System.out.print("FizzBuzz");
            else if (i % 3 == 0)//fizz coutner
                System.out.print("Fizz");
            else if (i % 5 == 0)// buzz
                System.out.print("Buzz"); 
            else
                System.out.print(i);


        }
    }

}

could anyone tell me why my print looks like a shrek code with 78 and 1314 etc when i want it to stop counting after 20? and please explain the mistakes im pretty new to this =) thanks in advance

1

There are 1 answers

3
sanbhat On

You should use

System.out.println(..);

instead of

System.out.print(..);

so that a new line is appended after each print() call