お正月に実家に帰省中に小二の姪っ子が宿題で九九の勉強をしているのを横で聞きながら「Javaで書いてみよう」と思ったので書いてみた。
for文で九九
for (int col = 1; col <= 9; col++) {
for (int row = 1; row <= 9; row++) {
System.out.println(col + " × " + row + " = " + (col * row));
}
}
これだけだと面白くないので、勉強中のStreamAPIで書いてみた。
StreamAPIで九九
IntStream.rangeClosed(1, 9)
.forEach(col -> IntStream.rangeClosed(1, 9)
.forEach(row -> System.out.println(col + " × " + row + " = " + (col * row)))
);
「単純なfor文をStreamAPIでどう書けばいいか」と「for文のネストとStreamAPIのネスト」が理解できた。