您可以计算给定数据类型的索引、最大值、因子和偏移量的值。有了这些信息,您就可以生成每一行,无论您在哪个索引。
TablePrinter.java
public class TablePrinter {
public static interface RowPrinter {
String print(int rowIndex, int dataIndex, Object[][] data);
}
public static void main(String[] args) {
// { label, max, factor, offset }
Object[][] data = {
{ "angles", 75, 5, 10 },
{ "meters", 30, 3, 12 }
};
printTable(data, new RowPrinter() {
@Override
public String print(int rowIndex, int dataIndex, Object[][] data) {
int max = (int) data[dataIndex][1];
int factor = (int) data[dataIndex][2];
int offset = (int) data[dataIndex][3];
return String.format(" | %8d", calculate(rowIndex, max, factor, offset));
}
});
}
public static void printTable(Object[][] data, RowPrinter rowPrinter) {
// Print headers
System.out.printf("%8s", "index");
for (int i = 0; i < data.length; i++) {
System.out.printf(" | %8s", data[i][0]);
}
System.out.println();
// Print data rows
for (int i = 0; i < 75; i++) {
System.out.printf("%8d", i + 1);
for (int j = 0; j < data.length; j++) {
System.out.printf(rowPrinter.print(i, j, data));
}
System.out.println();
}
}
public static int calculate(int index, int max, int factor, int offset) {
return ((index * factor) % (max - offset + factor)) + offset;
}
}
结果
index | angles | meters
1 | 10 | 12
2 | 15 | 15
3 | 20 | 18
4 | 25 | 21
5 | 30 | 24
6 | 35 | 27
7 | 40 | 30
8 | 45 | 12
9 | 50 | 15
10 | 55 | 18
11 | 60 | 21
12 | 65 | 24
13 | 70 | 27
14 | 75 | 30
15 | 10 | 12
16 | 15 | 15
17 | 20 | 18
18 | 25 | 21
19 | 30 | 24
20 | 35 | 27
21 | 40 | 30
22 | 45 | 12
23 | 50 | 15
24 | 55 | 18
25 | 60 | 21
26 | 65 | 24
27 | 70 | 27
28 | 75 | 30
29 | 10 | 12
30 | 15 | 15
31 | 20 | 18
32 | 25 | 21
33 | 30 | 24
34 | 35 | 27
35 | 40 | 30
36 | 45 | 12
37 | 50 | 15
38 | 55 | 18
39 | 60 | 21
40 | 65 | 24
41 | 70 | 27
42 | 75 | 30
43 | 10 | 12
44 | 15 | 15
45 | 20 | 18
46 | 25 | 21
47 | 30 | 24
48 | 35 | 27
49 | 40 | 30
50 | 45 | 12
51 | 50 | 15
52 | 55 | 18
53 | 60 | 21
54 | 65 | 24
55 | 70 | 27
56 | 75 | 30
57 | 10 | 12
58 | 15 | 15
59 | 20 | 18
60 | 25 | 21
61 | 30 | 24
62 | 35 | 27
63 | 40 | 30
64 | 45 | 12
65 | 50 | 15
66 | 55 | 18
67 | 60 | 21
68 | 65 | 24
69 | 70 | 27
70 | 75 | 30
71 | 10 | 12
72 | 15 | 15
73 | 20 | 18
74 | 25 | 21
75 | 30 | 24