Topic: 求助在作两整数相除时如何在输出时显示为有理数的形式,而不是计算结果

  Print this page

1.求助在作两整数相除时如何在输出时显示为有理数的形式,而不是计算结果 Copy to clipboard
Posted by: zmj888
Posted on: 2005-12-04 20:14

求助在作两整数相除时如何在输出时显示为有理数的形式,而不是计算结果
比如
int a=1,b=2;
System.out.println(a/b)//显示结果为0
我要显示为1/2怎么办?我想了好久没有想到办法,求高手赐教

2.Re:求助在作两整数相除时如何在输出时显示为有理数的形式,而不是计算结果 [Re: zmj888] Copy to clipboard
Posted by: why
Posted on: 2005-12-04 21:42

This topic seems to be a sequel of http://www.cjsdn.net/post/view?bid=1&id=164560

You could use the toString method of your Rational class in that post.
i.e. (new Rational(a, b)).toString()

or you could add a static method in your Rational class for formatting 有理数
and use
Rational.staticMethodToFormat(a, b)

something like
public static String staticMethodToFormat(int a, int b) {
return (a + "/" + b); // could have used StringBuffer
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923