Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@simonharrer
Last active June 4, 2018 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonharrer/db88e45af964e71ae9db04fb7e1cb7aa to your computer and use it in GitHub Desktop.
Save simonharrer/db88e45af964e71ae9db04fb7e1cb7aa to your computer and use it in GitHub Desktop.
FizzBuzz 4 spaces indent with an increase of 4 spaces with the first three indents without an increase
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