【发布时间】:2020-06-25 15:52:24
【问题描述】:
如何编写一个程序来计算所有小于或等于 N 的数字的乘法表。请注意,N 是从用户读取的整数。
程序会重复
这样做直到用户在 JAVA 中输入 -1。
我不知道我是否应该为此使用嵌套循环或方法,但我编写了以下未完成的代码,这给了我一个无限循环
public static void main(String[] args) {
int N ;
System.out.println("Enter N: " );
N = in.nextInt();
while ( N != -1) {
for(int i = 1; i <= N; ++i)
{
for (int c = 1; c <= 10; ++c)
System.out.println(N + "*" + c + " = " + (N*c));
}
}
}
我想要这样的输出:
Enter an integer to print it's multiplication table, -1 to
exit
2
Multiplication table of 1
1*1 = 1, 1*2 = 2, 1*3 = 3, 1*4 = 4, 1*5 = 5, 1*6 = 6, 1*7 =
7, 1*8 = 8, 1*9 = 9, 1*10 = 10,
Multiplication table of 2
2*1 = 2, 2*2 = 4, 2*3 = 6, 2*4 = 8, 2*5 = 10, 2*6 = 12, 2*7
= 14, 2*8 = 16, 2*9 = 18, 2*10 = 20,
Enter an integer to print it's multiplication table, -1 to
exit
-1
【问题讨论】:
标签: java loops for-loop methods while-loop