-1

Can anyone tell me why my program is giving me a output of fizz9 instead of just fizz?

import java.util.Scanner;
/**
 * Write a description of class fizzbuzz here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class FizzBuzz
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        boolean fizz = n % 3 == 0;
        boolean buzz = n % 5 == 0;

I can only use fizz and buzz in the conditions.

        if (fizz)

        {
           System.out.println("fizz"); 
        }
        if (buzz)
        {
            System.out.println("buzz"); 
        }
        else
        {
            System.out.println(n); 

        }
        System.out.println();
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

The problem is your else statement. If n is a multiple of 3 (but not 5), fizz will be true (thus printing "fizz"). However, buzz will be false (thus printing n).

Are you sure you want to print n at all? Maybe you meant to check to make sure that both fizz and buzz were false? (If so, you should change it from else to else if (!fizz)).