这次的作业要求写一个分数矩阵类,包含矩阵的加法、减法、乘法,已经以分数形式输出的print方法
我是新手啊,只能把矩阵打印成下面这种形式了,好丑
[  0   1  ]
[ 1/2  1  ][ 1/2 5/2 ]
[ 1/2 3/2 ]
[  0  1/2 ]我使用的代码是这样的 /* M[print Fraction Matrix] */
public void printFraction() { for (int i = 0; i < nrows; i++) {
System.out.print("[ ");
for (int j = 0; j < ncols; j++) {
rValue[i][j].printFraction();
System.out.print(" ");
}
System.out.print("]");
System.out.print("\n");
}
System.out.print("\n");
}
逐行输出的就只能做到这样了
请教各位高手有没有更美观的输出方式?
能够跨越很多行把数阵打印在一对大的方括号里面的?求方法,谢谢!

解决方案 »

  1.   

    补充一下:
    上面的代码运用到了一个自己写的分数类(也是作业要求的)
    其中打印分数的代码我是这样写的: /* [print a/b] */
    public void printFraction() {
    int a = numerator;
    int b = denominator;
    if (a == 0) {
    System.out.print(" 0 ");
    } else if (b == 1) {
    System.out.print(" " + a + " ");
    } else {
    System.out.print(a + "/" + b);
    }
    }
      

  2.   

    没办法打印跨行的 大括号的
    不过楼的分数类的打印可以改成下边这样  貌似可以好看点public void printFraction() {
            int a = numerator;
            int b = denominator;
            if (a == 0) {
                System.out.print("0" + "\t");
            } else if (b == 1) {
                System.out.print(a + "\t");
            } else {
                System.out.print(a + "/" + b + "\t");
            }
        }
      

  3.   


    soga,水平制表符的运用啊,这样可能会把矩阵打印得很宽,但是确实整齐了。
    我觉得不应该把它加在分数类的printFraction()里,而应该加在矩阵类override的printFraction()里,现在这样操作以后确实美观了一些,非常感谢啊!
      

  4.   


    没有任何方式可以实现么?我有想过会不会可以通过Swing来实现,但是这个我完全没有学过不知道可不可行。