# Java Fundamentals 1 Exercise 8 - with Zero value check

It’s a little long but implements DRY

public class Main {

public static void main(String[] args) {
/* Requirements:
*  If number is divisible by 5, print Fizz
*  If number is divisible by 3, print Buzz
*  If number is divisible by both 5 and 3, print FizzBuzz
*  If number is not divisible by both 5 or 3, print the same number
*/
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

boolean isGreaterThanZero = number > 0;
boolean isDivisibleByThree = isGreaterThanZero && number % 3 == 0;
boolean isDivisibleByFive = isGreaterThanZero && number % 5 == 0;
boolean isDivisibleByThreeAndFive = isDivisibleByThree && isDivisibleByFive;

if (isDivisibleByThreeAndFive)
System.out.println("FizzBuzz");
else if (isDivisibleByFive)
System.out.printf("Fizz");
else if (isDivisibleByThree)
System.out.println("Buzz");
else
System.out.println(number);
}

}

Were you looking for commentary? Honestly, I think storing all of those booleans in variables is a waste of time. It is much more readable to just have the comparisons in the if/else statements. Also, you still have duplication. This is about the shortest way to write it:

// Honestly, do we even need this part?
// It is not common for the FizzBuzz project
if (number <= 0) {
System.out.println(number);
return;
}
var sb = new StringBuilder();
if (number % 5 == 0) {
sb.append("Fizz");
}
if (number % 3 == 0) {
sb.append("Buzz");
}
if (sb.isEmpty()) {
sb.append(number);
}
System.out.println(sb.toString());