LoginSignup
1
0

More than 5 years have passed since last update.

Javaで九九を出力してみた

Last updated at Posted at 2019-01-04

お正月に実家に帰省中に小二の姪っ子が宿題で九九の勉強をしているのを横で聞きながら「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のネスト」が理解できた。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0