BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #18764同步于 2011/6/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

新手求教~~

wawabing
2011/6/10镜像同步8 回复
public class TestRational{ public static void main(String[] args){ String output=""; Rational r1=new Rational(0,1); for(int i=1;i<100;i++){ Rational r2=new Rational(i,i+1); r1=r1.add(r2); if(i<99) output+=r2.toString()+"+"; else output+=r2.toString()+"="; } output+=r1.toString(); System.out.println(output); } } Rational: public class Rational extends Number implements Comparable { private long numerator = 0; private long denominator = 1; public Rational() { this(0, 1); } public Rational(long numerator, long denominator) { long gcd = gcd(numerator, denominator); this.numerator = ((denominator>0)?1:-1)*numerator / gcd; this.denominator = Math.abs(denominator) / gcd; } private static long gcd(long n, long d) { long n1=Math.abs(n); long n2=Math.abs(d); int gcd = 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1% k == 0 && n2% k == 0) gcd = k; } return gcd; } public long getNumerator() { return numerator; } public long getDenominator() { return denominator; } public Rational add(Rational secondRational) { long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); } public Rational subtract(Rational secondRational) { long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator(); long d = denominator * secondRational.getDenominator(); return new Rational(n, d); } public String toString() { if (denominator == 1) return numerator + ""; else return numerator + "/" + denominator; } public int intValue() { return (int) doubleValue(); } public float floatValue() { return (float) doubleValue(); } public long longValue() { return (long) doubleValue(); } public double douleValue() { return numerator * 1.0 / denominator; } public int compareTo(Object o) { if ((this.subtract((Rational) o)).getNumerator() > 0) return 1; else if ((this.subtract((Rational) o)).getNumerator() < 0) return -1; else return 0; } @Override public double doubleValue() { // TODO Auto-generated method stub return 0; } } 运行之后 Exception in thread "main" java.lang.ArithmeticException: / by zero at Rational.gcd(Rational.java:20) at Rational.<init>(Rational.java:10) at Rational.add(Rational.java:38) at TestRational.main(TestRational.java:8) 是不是main函数计算出错了呢 有点迷糊了 求教啊 谢谢[em43]
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
buptlong机器人#1 · 2011/6/10
这是要计算1/2+...+n/n+1 ,n=1,...,99 ? 【 在 wawabing (娃娃兵) 的大作中提到: 】 : public class TestRational{ : public static void main(String[] args){ : String output=""; : ...................
wawabing机器人#2 · 2011/6/10
恩 是啊 计算1/2+...99/100的和 特别菜鸟 刚开始学 【 在 buptlong 的大作中提到: 】 : 这是要计算1/2+...+n/n+1 ,n=1,...,99 ?
laoboss机器人#3 · 2011/6/10
你这是在同一个文件中么,在同一个文件中,不能存在两个public的类。
wawabing机器人#4 · 2011/6/10
是在一个文件里 分开写的两个类 那我改改试试 谢谢哈 【 在 laoboss 的大作中提到: 】 : 你这是在同一个文件中么,在同一个文件中,不能存在两个public的类。
laoboss机器人#5 · 2011/6/10
【 在 wawabing 的大作中提到: 】 : 是在一个文件里 分开写的两个类 那我改改试试 谢谢哈 : 【 在 laoboss 的大作中提到: 】 : : 你这是在同一个文件中么,在同一个文件中,不能存在两个public的类。 : ................... 呵呵,我也是新手,多交流。
wawabing机器人#6 · 2011/6/10
还是不可以的 和之前的错误一样 我觉得是我main函数计算的错误啊 晕了 【 在 laoboss 的大作中提到: 】 : 呵呵,我也是新手,多交流。
laoboss机器人#7 · 2011/6/10
【 在 wawabing 的大作中提到: 】 : 还是不可以的 和之前的错误一样 我觉得是我main函数计算的错误啊 晕了 : 【 在 laoboss 的大作中提到: 】 : : 呵呵,我也是新手,多交流。 : ................... 你是想实现0+1+1/2+...+n-1/n的值的通分求和?我运行了一下,没有问题。我在你的循环体中加了System.out.println(r1); System.out.println(output); 来观察程序运行,有如下的结果输出: 1/2 7/6 1/2+ 23/12 1/2+2/3+ 163/60 1/2+2/3+3/4+ 71/20 1/2+2/3+3/4+4/5+ 617/140 1/2+2/3+3/4+4/5+5/6+ 1479/280 1/2+2/3+3/4+4/5+5/6+6/7+ 15551/2520 1/2+2/3+3/4+4/5+5/6+6/7+7/8+ 17819/2520 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+ 221209/27720 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+ 246619/27720 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+ 3538687/360360 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+ 3873307/360360 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+12/13+ 4209643/360360 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+12/13+13/14+ 9094961/720720 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+12/13+13/14+14/15+ 166145857/12252240 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+12/13+13/14+14/15+15/16+ 59239139/4084080 1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+9/10+10/11+11/12+12/13+13/14+14/15+15/16+16/17+ 1199057081/77597520 ...... 结果似乎还有点问题,而且你的程序运行过慢,几乎要卡死我的机器了,我觉得跑不到一百了,这才跑到17,呃(我还改了String 类为StringBuffer,要不然真卡爆的)。
wawabing机器人#8 · 2011/6/10
哦 明白了 哈哈 多谢了~ 【 在 laoboss 的大作中提到: 】 : 你是想实现0+1+1/2+...+n-1/n的值的通分求和?我运行了一下,没有问题。我在你的循环体中加了System.out.println(r1); : System.out.println(output); : 来观察程序运行,有如下的结果输出: : ...................