Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Javaで九九を出力してみた

More than 1 year has passed since last update.

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

kazokmr
自分のための記録用です
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away