Skip to content

Instantly share code, notes, and snippets.

@simonharrer
Last active June 4, 2018 07:26
Show Gist options
  • Select an option

  • Save simonharrer/d96c2864477067aae20fcd288b328243 to your computer and use it in GitHub Desktop.

Select an option

Save simonharrer/d96c2864477067aae20fcd288b328243 to your computer and use it in GitHub Desktop.
FizzBuzz4Spaces
class FizzBuzz {
public static void main(String[] args) {
if (args != null && args.length == 1) {
try {
int number = Integer.parseInt(args[0]);
for (int i = 1; i < number; 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);
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("please pass in a number", e);
}
} else {
throw new IllegalArgumentException("please pass in a number");
}
}
}
class FizzBuzz {
public static void main(String[] args) {
if (args == null || args.length != 1) {
throw new IllegalArgumentException("please pass in a number");
}
int number = parse(args[0]);
for (int i = 1; i < number; i++) {
System.out.println(numberToFizzBuzz(i));
}
}
private static int parse(String number) {
try {
return Integer.parseInt(number);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("please pass in a number", e);
}
}
private static String numberToFizzBuzz(int i) {
if (i % (3 * 5) == 0) {
return "FizzBuzz";
} else if (i % 3 == 0) {
return "Fizz";
} else if (i % 5) {
return "Buzz";
} else {
return String.valueOf(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment