1)
int product = r * c;
if (product < 10) {
System.out.print(" " + product + " ");
} else {
System.out.print(product + " ");
}
We have 2 numbers, r and c. We have no info about their changes in (1) (this code snippet), that means, they are constant here. We find their product. We print this product so, that it always take 2 symbols and there is a space after the last symbol.
An arbitrary example is, say "24 " or " 4 ".
2)
for (int c = 1; c <= 10; c++) {
System.out.println();
// (1) snippet goes here
}
Here we have c changing from 1 to 10. Then we have println() - that means, everything after this statement will be printed on the next row. And the (1) code executes 10 times for each c from 1 to 10. (1) simply prints the product of c and r. So far, we have no info about r, that means, that r is constant (so far). That means, that this code snippet (2) will print 1*r, 2*r, ..., 10*r in a single line (see (1)) for a given r.
for (int r = 1; r <= 10; r++) {
// (2) snippet goes here
}
r changes from 1 to 10. For each r (2) snippet is executed. What (2) does? It prints new line and prints the products of all the numbers from 1 to 10 and given number r.
Also, following code illustrates my explanations in a clearer way:
public static void main(String[] args) {
for (int r = 1; r <= 10; r++) {
System.out.println();
productOfAllNumbersBefore10AndR(r);
}
}
public static void productOfAllNumbersBefore10AndR(int r) {
for (int c = 1; c <= 10; c++) {
printProductOfTwoNumbersInANiceWay(r, c);
}
}
public static void printProductOfTwoNumbersInANiceWay(int r, int c) {
int product = r * c;
if (product < 10) {
System.out.print(" " + product + " ");
} else {
System.out.print(product + " ");
}
}
- For each number from 1 to 10 (r), print productOfAllNumbersBefore10AndR
- How to print productOfAllNumbersBefore10AndR ?
- To do this, from the next row (println()), for each number from 1 to 10 (c) printProductOfTwoNumbersInANiceWay, and that two numbers are r and c
- How do I do this?
- First you find the product, then you print it - but with print(), not println(): you don't want a trailing carriage return.