英文:
how do I print a mathematical times table in java (the Whole times table of a certain number)
问题
我正在学习编程,最近遇到了一个挑战,要求按用户输入的某个数字打印数学乘法表。假设输入的数字是10,则应按如下打印表格。
1 * 10 = 10
2 * 10 = 20
3 * 10 = 30
4 * 10 = 40
5 * 10 = 50
6 * 10 = 60
7 * 10 = 70
8 * 10 = 80
9 * 10 = 90
10 * 10 = 100
我成功地使用以下代码打印了输入数字的乘法表,但没有打印整个表格。
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int value = 1;
for (int i = 1; i <= input; i++) {
value = i * input;
System.out.println(value);
}
输出结果:
10
20
30
40
50
60
70
80
90
100
如果我也能学会在Python和PHP中完成相同的操作,将会很有帮助。
英文:
I am learning programing and recently got a challenge to print a mathematical times table of a certain number entered by a user. Lets say the number entered is 10, then the table should be printed as below.
I managed to print the times table of the entered number with the below codes. But not the whole table of that number.
Scanner scan= new Scanner(System.in);
int input= scan.nextInt();
int value=1;
for(int i=1; i<=input; i++){
value= i*input;
System.out.println(value);
}
Output:
10
20
30
40
50
60
70
80
90
100
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Kindly Assist,it will also be helpful if i can learn to do the same in python and PHP.
答案1
得分: 2
打印矩阵形式的解决方案的思路是,使用两个嵌套的for循环,矩阵的大小为(input x input)。
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
for(int i=1; i<=input; i++){
for(int j=1; j<=input; j++){
System.out.print(i*j + " ");
}
System.out.print("\n");
}
输入为10时的输出结果如下:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
英文:
The idea of your output is to print a matrix form solution, so the better option is to use two for nested loops, the matrix that you will print is (input x input) size.
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
for(int i=1; i<=input; i++){
for(int j=1; j<=input; j++){
System.out.print(i*j + " ");
}
System.out.print("\n");
}
This will be the output for input 10:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论