Skip to content

Instantly share code, notes, and snippets.

@koduki
Created May 24, 2014 12:31
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 koduki/ae220a859e0c6d1e5c78 to your computer and use it in GitHub Desktop.
Save koduki/ae220a859e0c6d1e5c78 to your computer and use it in GitHub Desktop.
FizzBuzz Java8対応版
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