Last active
June 4, 2018 07:27
-
-
Save simonharrer/cd838c2bf9accc49131ab30ce0dd69ba to your computer and use it in GitHub Desktop.
FizzBuzz 4 spaces indent with an increase of 4 spaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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