Created
May 24, 2014 12:31
-
-
Save koduki/ae220a859e0c6d1e5c78 to your computer and use it in GitHub Desktop.
FizzBuzz Java8対応版
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
import java.util.stream.IntStream; | |
/** | |
* | |
* @author koduki | |
*/ | |
public class FizzBuzz { | |
public static void main(String[] args) { | |
fizzBuzzWithOldStyle(); | |
fizzBuzzWithNewStyle(); | |
} | |
/** | |
* Java 8 Style. | |
*/ | |
public static void fizzBuzzWithNewStyle() { | |
//fizzBuzzWithOldStyle(); | |
IntStream.rangeClosed(1, 100).boxed() | |
.map(n | |
-> ((n % 3 == 0) && (n % 5 == 0)) ? "FizzBuzz" | |
: (n % 3 == 0) ? "Fizz" | |
: (n % 5 == 0) ? "Buzz" | |
: n.toString()) | |
.forEach((x) -> System.out.println(x)); | |
} | |
/** | |
* Before Java 7 Style. | |
*/ | |
public static void fizzBuzzWithOldStyle() { | |
for (int i = 1; i <= 100; 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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment