While I was returning home on New Year's Day, I heard that Koji's niece was studying multiplication tables for homework, and thought, "Let's write in Java," so I wrote it.
Multiplication table with for statement
for (int col = 1; col <= 9; col++) {
for (int row = 1; row <= 9; row++) {
System.out.println(col + " × " + row + " = " + (col * row));
}
}
This alone is not interesting, so I wrote it with the Stream API I am studying.
Multiplication table with Stream API
IntStream.rangeClosed(1, 9)
.forEach(col -> IntStream.rangeClosed(1, 9)
.forEach(row -> System.out.println(col + " × " + row + " = " + (col * row)))
);
I understood "how to write a simple for statement with Stream API" and "nesting of for statement and nesting of Stream API".
Recommended Posts