topic:
The input n, n row print multiplication tables.
You need to use each two-dimensional array of strings stored multiplication tables, such as storage 1*1=1
.
In order to ensure that procedures used in the two-dimensional array, to be used after printing the multiplication tables Arrays.deepToString
content to print a two-dimensional array.
Reminder: output format may be used String.format
or System.out.printf
.
Output Format Description
- Each end of the line with no spaces.
- Between each of the expression (counting from the first character of an expression between the first character of the next expression), a total of 7 characters. As
2*1=2 2*2=4
from the first to the second one 2`2*2=4
among the first letter, a total of seven characters (including spaces, in this example contains two spaces).
Sample input:
2
5
Sample output:
1*1=1
2*1=2 2*2=4
[[1*1=1], [2*1=2, 2*2=4]]
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
[[1*1=1], [2*1=2, 2*2=4], [3*1=3, 3*2=6, 3*3=9], [4*1=4, 4*2=8, 4*3=12, 4*4=16], [5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25]]
代码:
1 import java.util.Scanner; 2 import java.util.Arrays; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 while(sc.hasNextInt()) { 7 int n = sc.nextInt(); 8 String[][] arr = new String[n][]; 9 for(int i = 0;i < n;i++) { 10 arr[i] = new String[i+1]; 11 for(int j = 0;j < i+1;j++) { 12 arr[i][j] = (i+1)+"*"+(j+1)+"="+(i+1)*(j+1); 13 if(j<i) 14 System.out.printf("%-7s",arr[i][j]); 15 else if(j==i) 16 System.out.printf("%s",arr[i][j]); 17 } 18 System.out.println(); 29 } 20 System.out.println(Arrays.deepToString(arr)); 21 } 22 } 23 }
笔记:
1.格式限定每个表达式之间包含7个字符,因此想到了格式化输出,用“%-7s”控制字符串占七个字符位,负号表示左对齐
int [ ] [ ] ARR ;
2.动态创建二维数组:
ARR = new new int [a dimension ] [ ] ; // dynamically creating a first dimension
for (I = 0 ; I <a dimension ; I ++ ) {
ARR [I ] = new new int [number-D ] ; // dynamically create a second dimension
for (J = 0 ; J <dimensional number ; J ++ ) {
ARR [I ] [J ]= j;
}
}