这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。
import java.math.BigInteger;
public class Rational {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** Construct a rational with specified numerator and denominator */
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd=new BigInteger(String.valueOf(gcd(numerator,
denominator)));
BigInteger r1=new
BigInteger(String.valueOf(denominator.compareTo(BigInteger.ZERO)));
this.numerator = (r1.multiply(numerator)).divide(gcd);
this.denominator = (denominator.abs()).divide(gcd);
}
/** Find GCD of two numbers */
private static long gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
int gcd = 1;
for (int k = 1; (new BigInteger(String.valueOf(k))).compareTo(n1)<=0 &&
(new BigInteger(String.valueOf(k))).compareTo(n2)<=0; k++) {
if (n1.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO) &&
n2.mod(new BigInteger(String.valueOf(k))).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
/** Return numerator */
public BigInteger getNumerator() {
return numerator;
}
/** Return denominator */
public BigInteger getDenominator() {
return denominator;
}
/** Add a rational number to this rational */
public Rational add(Rational secondRational) {
BigInteger n =
numerator.multiply(secondRational.getDenominator())
.add(denominator.multiply(sec
ondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Subtract a rational number from this rational */
public Rational subtract(Rational secondRational) {
BigInteger n =
(numerator.multiply(secondRational.getDenominator()))
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Multiply a rational number to this rational */
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
/** Divide a rational number from this rational */
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
/** Compute the square of this rational number*/
public Rational square() {
BigInteger n = numerator.multiply(numerator);
BigInteger d = denominator.multiply(denominator);
return new Rational(n, d);
}
/** toString */
public String toString() {
return numerator + "/" + denominator;
}
}
这是 testRational 类 import java.math.BigInteger;
public class TestRational {
public static void main(String[]args){
int y = 1;
BigInteger i=new BigInteger(String.valueOf(1));
BigInteger a=new BigInteger(String.valueOf(2));
BigInteger b=new BigInteger(String.valueOf(3));
BigInteger c=new BigInteger(String.valueOf(5));
Rational sum = new Rational(BigInteger.ZERO,a);
Rational r0 = new Rational(b,b.add(i));
Rational r2 = new Rational(a,c);
Rational r3 = new Rational(a,c);
Rational s1 = r3.multiply(r2);
Rational s2 = r3.square();
Rational s3 = r2.divide(r3);
Rational r1 = new Rational(i,a);
do{
sum = sum.add(r0);
b = b.add(a);
y++;
}while(y<49);
System.out.println(sum.multiply(sum));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(r0);
}
}
这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。这段代码的目的是计算 (1/2+3/4+...+99/100)^2。但是我的循环无法正确执行。r1 的结果是 3/4 而不是 99/100,我的代码有什么问题?我认为我的循环可以运行,因为我可以正确地得到它。那么如何更正我的代码并使其能够计算 (1/2+3/4+...+99/100)^2 ?谢谢你的回答。