需要打印九九乘法表 格式如下:
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
没有写成单独的方法,不过也还可以的~public class MultTable { /**
* @param args
*/
public static void main(String[] args) {
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.print("* ");
}else if(i== 0&& j!= 0){
System.out.print(j+ " ");
}else if(i!= 0&& j==0){
System.out.print(i+ " ");
}else if(i>= j){
System.out.print((j* i)+ " ");
}
}
System.out.println();
}
System.out.println();
}
}
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
printFirstLine();
printOthers();
} private static void printOthers() {
for (int i = 1; i < 10; i++) {
printLine(i);
}
} private static void printLine(int n) {
String str = n + " ";
for (int i = 1; i <= n; i++) {
str += (i * n) + ((i * n) >= 10? " ": " ");
}
System.out.println(str);
} private static void printFirstLine() {
System.out.print("* ");
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}
* @param args
*/
public static void main(String[] args) {
for(int i = 1; i < 10;i++)
for(int j = 1 ;j <= i ;j++)
{
if(i==j)
{
System.out.print(i*j+" ");
System.out.println();
}
else
System.out.print(i*j+" ");
}
}}
public static void main(String[] args) {
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.printf(" *");
}else if(i== 0&& j!= 0){
System.out.printf("%4d",j);
}else if(i!= 0&& j==0){
System.out.printf("%4d",i);
}else if(i>= j){
System.out.printf("%4d",(j* i));
}
}在jdk输出为:
C:\>java MultTable
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81在2楼的基础上增加格式化输出。
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.printf(" *");
}else if(i== 0&& j!= 0){
System.out.printf("%4d",j);
}else if(i!= 0&& j==0){
System.out.printf("%4d",i);
}else if(i>= j){
System.out.printf("%4d",(j* i));
}
}在jdk1.5输出为:
C:\>javac MultTable.javaC:\>java MultTable
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
在2楼基础上加一个格式化输出。