tables.java
来自「Beginning Java 2, SDK 1.4 Edition Exerci」· Java 代码 · 共 38 行
JAVA
38 行
//Chapter 4, Exercise 2
public class Tables {
public static void main(String[]args) {
final int TABLE_SIZE = 12;
// Declare the rectangular array to store the multiplication table:
int[][] table = new int[TABLE_SIZE][TABLE_SIZE];
// Fill in the array with the multiplication table:
for(int i = 0 ; i<table.length ; i++) {
for(int j = 0 ; j<table[i].length ; j++) {
table[i][j] = (i+1)*(j+1);
}
}
// Output the table heading
System.out.print(" :"); // Row name column heading
for(int j = 1 ; j<=table[0].length ; j++) {
System.out.print((j<10 ? " ": " ") + j);
}
System.out.println("\n-------------------------------------------------------");
// Output the table contents
// Each entry in the table should be four characters wide so we output
// three spaces preceding values less than 10, two spaces preceding values
// from 10 to 99 and one space for values exceeding 100.
for(int i=0; i<table.length; i++) {
System.out.print("Row" + (i<9 ? " ":" ") + (i+1) + ":");
for(int j=0; j<table[i].length; j++) {
System.out.print((table[i][j]<10 ? " " : table[i][j]<100 ? " " : " ") + table[i][j]);
}
System.out.println();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?