Created
April 27, 2014 02:45
-
-
Save toshi0383/11336506 to your computer and use it in GitHub Desktop.
ifとかswitchを使わずにFizzBuzz ref: http://qiita.com/toshi0383/items/c32de4329e0fbb71b511
This file contains 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
/** | |
* FizzBuzz | |
*/ | |
public class FizzBuzz { | |
final String F = "Fizz"; | |
final String B = "Buzz"; | |
public void fizzBuzz(int number) { | |
for (int i = 1; i < number + 1; i++) { | |
boolean fizz = (i % 3 == 0); | |
boolean buzz = (i % 5 == 0); | |
if (fizz && buzz) { | |
output(F); | |
} else if (fizz) { | |
output(B); | |
} else if (buzz) { | |
output(F + B); | |
} else { | |
output(Integer.toString(i)); | |
} | |
} | |
} | |
private void output(String str) { | |
System.out.println(str); | |
} | |
public static void main(String[] args) { | |
new FizzBuzz().fizzBuzz(Integer.parseInt(args[0])); | |
} | |
} |
This file contains 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
/** | |
* FizzBuzz | |
*/ | |
public class FizzBuzz { | |
final String F = "Fizz"; | |
final String B = "Buzz"; | |
public void fizzBuzz(int number) { | |
for (int i = 1; i < number + 1; i++) { | |
boolean fizz = (i % 3 == 0); | |
boolean buzz = (i % 5 == 0); | |
if (fizz && buzz) { | |
output(F); | |
} else if (fizz) { | |
output(B); | |
} else if (buzz) { | |
output(F + B); | |
} else { | |
output(Integer.toString(i)); | |
} | |
} | |
} | |
private void output(String str) { | |
System.out.println(str); | |
} | |
public static void main(String[] args) { | |
new FizzBuzz().fizzBuzz(Integer.parseInt(args[0])); | |
} | |
} |
This file contains 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
/** | |
* FizzBuzz 2 | |
* if文使わないバージョン | |
*/ | |
public class FizzBuzz2 { | |
final String F = "Fizz"; | |
final String B = "Buzz"; | |
public void fizzBuzz(int number) { | |
for (int i = 1; i < number + 1; i++) { | |
boolean fizz = (i % 3 == 0); | |
boolean buzz = (i % 5 == 0); | |
System.out.println( | |
fizz && buzz ? F + B | |
: fizz ? F | |
: buzz ? B | |
: Integer.toString(i) | |
); | |
} | |
} | |
public static void main(String[] args) { | |
new FizzBuzz2().fizzBuzz(Integer.parseInt(args[0])); | |
} | |
} |
This file contains 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
/** | |
* FizzBuzz 2 | |
* if文使わないバージョン | |
*/ | |
public class FizzBuzz2 { | |
final String F = "Fizz"; | |
final String B = "Buzz"; | |
public void fizzBuzz(int number) { | |
for (int i = 1; i < number + 1; i++) { | |
boolean fizz = (i % 3 == 0); | |
boolean buzz = (i % 5 == 0); | |
System.out.println( | |
fizz && buzz ? F + B | |
: fizz ? F | |
: buzz ? B | |
: Integer.toString(i) | |
); | |
} | |
} | |
public static void main(String[] args) { | |
new FizzBuzz2().fizzBuzz(Integer.parseInt(args[0])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment