第一章第四题(打印表格)(Print a table)
- 1.4(打印表格)编写程序,显示以下表格:
a | a^2 | a^3 |
---|---|---|
1 | 1 | 1 |
2 | 4 | 8 |
3 | 9 | 27 |
4 | 16 | 64 |
1.4 (Print a table) Write a program that displays the following table:
a | a^2 | a^3 |
---|---|---|
1 | 1 | 1 |
2 | 4 | 8 |
3 | 9 | 27 |
4 | 16 | 64 |
-
参考代码:
-
方法一:
package chapter01; public class Code_04 { public static void main(String[] args){ int[][] numbers={ {1,1,1}, {2,4,6}, {3,9,27}, {4,16,64} }; System.out.println("a a^2 a^3"); for (int i=0;i<4;i++){ for(int j=0;j<3;j++){ System.out.format("%-6d",numbers[i][j]); } System.out.println(" "); } } }
-
方法二:
public class PrintTableQuestion4 { public static void main(String[] args) { System.out.println("a a^2 a^3"); System.out.println(1 + " " + 1 * 1 + " " + 1 * 1 * 1); System.out.println(2 + " " + 2 * 2 + " " + 2 * 2 * 2); System.out.println(3 + " " + 3 * 3 + " " + 3 * 3 * 3); System.out.println(4 + " " + 4 * 4 + " " + 4 * 4 * 4); } }
-
-
结果显示:
a a^2 a^3
1 1 1
2 4 6
3 9 27
4 16 64
Process finished with exit code 0