九九乘法表
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题。《九九乘法歌诀》,又常称为“小九九”,如下图所示。你的任务是写一个程序,对于给定的一个正整数 n ,输出“九九乘法表”的前 n 行。例如,输入 n 为 9,你的程序的输出将为下图:
Input
输入包含多组测试数据,以 EOF 结束。每组测试数据只包含一个正整数 n (0 < n < 10)。
Output
对于每组测试数据,输出上图所示“九九乘法表”的前 n 行。
Sample Input
2
3
Sample Output
1*1=1
1*2=2 2*2=4
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
Hint
必须使用for循环,如果你的代码中出现例如
if(n == 1) printf(“1*1=1\\n”);
if(n == 2) printf(“1*1=1\\n1*2=2 2*2=4\\n”);
或其类似语句,本题不得分。
Source
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (i == j) {
System.out.println(j + "*" + i + "=" + i * j);
} else {
System.out.print(j + "*" + i + "=" + i * j + " ");
}
}
}
}
}
}