这次的作业要求写一个分数矩阵类,包含矩阵的加法、减法、乘法,已经以分数形式输出的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");
}
逐行输出的就只能做到这样了
请教各位高手有没有更美观的输出方式?
能够跨越很多行把数阵打印在一对大的方括号里面的?求方法,谢谢!
我是新手啊,只能把矩阵打印成下面这种形式了,好丑
[ 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");
}
逐行输出的就只能做到这样了
请教各位高手有没有更美观的输出方式?
能够跨越很多行把数阵打印在一对大的方括号里面的?求方法,谢谢!
解决方案 »
- 如何分割JFrame?
- “用鼠标拖动我”小程序的问题?
- 什么叫ParameterizedType?
- 单个 boolean 使用 int 来表示,占 4 个字节,而 boolean 数组是使用 byte 数组来表示的。 ???
- 不想回家过年了,想在北京找份java实习的工作。
- java 内存泄漏的问题,关于DatagramSocket.send和System.arraycopy
- 谁写的结构如此差的程序,该回家种田了
- 程序中import.*引入的是哪个包?C:\j2sdk1.4\jre还是C:\j2sdk1.4\lib
- 怎样设置JDK的初始环境?本来都已经设好了:(困惑。
- new java.sql.Date().getTime()
- java 快速排序算法 到底是錯在哪了
- tomcat问题
上面的代码运用到了一个自己写的分数类(也是作业要求的)
其中打印分数的代码我是这样写的: /* [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);
}
}
不过楼的分数类的打印可以改成下边这样 貌似可以好看点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");
}
}
soga,水平制表符的运用啊,这样可能会把矩阵打印得很宽,但是确实整齐了。
我觉得不应该把它加在分数类的printFraction()里,而应该加在矩阵类override的printFraction()里,现在这样操作以后确实美观了一些,非常感谢啊!
没有任何方式可以实现么?我有想过会不会可以通过Swing来实现,但是这个我完全没有学过不知道可不可行。