You can do it with the following algorithm:
- You have to do it
7
times and therefore you can use a loop that should run 7
times.
- Each row starts with the row number, and run for row number * row number times with a step-value equal to the row number.
Given below is the implementation of this algorithm using for
loop and I leave it to you to implement it using the do-while
loop (as it seems to be your homework ??)
public class Main {
public static void main(String[] args) {
int n = 7;
for (int row = 1; row <= n; row++) {
for (int col = row; col <= row * row; col += row) {
System.out.printf("%-4d", col);
}
System.out.println();
}
}
}
Output:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…