当前位置: 代码迷 >> Java Web开发 >> java输出矩形99乘法表时怎样控制对齐输出?解决思路
  详细解决方案

java输出矩形99乘法表时怎样控制对齐输出?解决思路

热度:8433   发布时间:2013-02-25 21:22:24.0
java输出矩形99乘法表时怎样控制对齐输出?
以下是代码:class Cheng
{
public static void main(String[]args)
{
int i,j;  
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
  System.out.print(j+"*"+i+"="+j*i+" ");  
}
}
System.out.println();
System.out.println();
  }
}
想要得到的结果是:
 1*1=1 1*1=2 1*3=3 ……

 2*1=1 2*2=2 2*3=3 ……

  ……

 9*1=1 9*2=18 9*3=27 ……

求各位帮忙,是不是有控制输出对齐的函数啊?


------解决方案--------------------------------------------------------
没的,有都是你自己写的..
System.out.println();放在第一个循环内,i,j换下不就可以啦嘛。

------解决方案--------------------------------------------------------
System.out.print(j+"*"+i+"="+j*i+" ");

改成

System.out.printf("%d * %d = %2d ", j, i, j * i);
------解决方案--------------------------------------------------------
System.out.print(j+"*"+i+"="+j*i+" ");
改成
System.out.print(j+"*"+i+"="+j*i+"\t");
\t表示制表符,有对齐的效果,LZ试试吧!


------解决方案--------------------------------------------------------
class Chen {
public static void main(String[]args) {
int i,j;
for(i=1;i<=9;i++) {
for(j=1;j<=9;j++) { 
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
}

你的System.out.println();位置也有问题。改好了LZ参考下吧

------解决方案--------------------------------------------------------
你这个不像99乘法表啊!!!!!
------解决方案--------------------------------------------------------
请参考下下面的代码
Java code
public static void main(String[] args) throws Exception {        int length=6;        String seperator="   ";        String format="%1$-"+length+"s"+seperator;        System.out.print("  ");        for(int i=1;i<10;i++){            System.out.print(String.format(format, i));        }        System.out.println();        for(int i=1;i<10;i++){            System.out.print(i+" ");            for(int j=1;j<=i;j++){                System.out.print(String.format(format, j+"*"+i+"="+(i*j)));            }            System.out.println();        }    }
------解决方案--------------------------------------------------------
public class test {
public static void main(String[] args) {
for(int i = 2, j = 1;i <= 10;i++){
if(j > 9){
j = 1;
System.out.println();
}else{
i--;
System.out.print(i+"*"+j+"="+i*j+" ");
j++;
}

}
}
}
------解决方案--------------------------------------------------------
没用过的同学可以试试,这和C中的printf是一样的,格式化输出。
探讨
System.out.print(j+"*"+i+"="+j*i+" ");

改成

System.out.printf("%d * %d = %2d ", j, i, j * i);

------解决方案--------------------------------------------------------
LS的那个格式化输出可以
还是第一次这样用,学到了
------解决方案--------------------------------------------------------
探讨
请参考下下面的代码

Java code


public static void main(String[] args) throws Exception {
int length=6;
String seperator=" ";
String format="%1$-"+length+"s"+seperator;
  相关解决方案