日期:2014-05-20  浏览次数:20870 次

java乘法表格式不标准怎么办?
i*j=sum 当sum单数和双数的时候显示占用位数不同,使显示出的结果不整洁.后面两个直接差得更大
问如何格式化System.out.print()的输出使输出更美观
请各位多帮忙

import java.io.*;

public class Project02{
public static void main(String[] args) {
print1();
print2();
print3();
print4();
}
public static void print1() {
int i,j;
System.out.println("First:");
for(i=1;i<=9;i++){
for(j=1;j<=i;j++)
System.out.print(i+"*"+j+"="+i*j+" ");
System.out.println();
}
}
public static void print2(){
int i,j;
System.out.println("Second:");
for(i=1;i<=9;i++){
for(j=1;j<=i;j++)
System.out.print(j+"*"+i+"="+j*i+" ");
System.out.println();
}
}
public static void print3(){
int i,j,k;
for(i=1;i<=9;i++){
for(k=1;k<=9-i-1;k++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(i+"*"+j+"="+i*j+" ");
System.out.println();
}
}
public static void print4(){
int i,j,k;
for(i=1;i<=9;i++){
for(k=1;k<=9-i-1;k++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(j+"*"+i+"="+j*i+" ");
System.out.println();
}
}
}

------解决方案--------------------
public static void print2(){ 
int i,j; 
System.out.println("Second:"); 
for(i=1;i <=9;i++){ 
for(j=1;j <=i;j++) 
System.out.printf("%d*%d=%-2d ",j,i,j*i); 
System.out.println(); 
}
------解决方案--------------------
luguo