gpt4 book ai didi

java - 我使用 BigInteger 创建了 Rational 类,但没有显示任何内容

转载 作者:行者123 更新时间:2023-12-01 18:09:39 25 4
gpt4 key购买 nike

import java.math.*;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int T = sc.nextInt();

for (int t = 0; t < T; t++) {
Rational r1 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));
Rational r2 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));

System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
}
}
}

class Rational extends Number implements Comparable<Rational> {
private static final long serialVersionUID = 1L;
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;

public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
// set numerator and denominator
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO) > 0) ? new BigInteger("1") : new BigInteger("-1"))
.multiply(numerator.divide(gcd));
this.denominator = denominator.abs().divide(gcd);
}
// Find GCD of two BigIntegers
private static BigInteger gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
BigInteger gcd = BigInteger.ONE;

for (BigInteger k = BigInteger.ONE; k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0; k.add(BigInteger.ONE)) {
if (n1.mod(k).equals(BigInteger.ZERO) && n2.mod(k).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}

public BigInteger getNumerator() {
return numerator;
}

public BigInteger getDenominator() {
return denominator;
}

public Rational add(Rational secondRational) {
BigInteger n = (numerator.multiply(secondRational.getDenominator()))
.add(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}

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);
}

public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}

public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}

@Override
public String toString() {
if (denominator.compareTo(BigInteger.ONE) == 0)
return numerator + "";
else
return numerator + "/" + denominator;
}

@Override
public boolean equals(Object other) {
if ((this.subtract((Rational) (other))).getNumerator().equals(BigInteger.ZERO))
return true;
else
return false;
}

@Override
public int intValue() {
return (int) doubleValue();
}

@Override
public float floatValue() {
return (float) doubleValue();
}

@Override
public double doubleValue() {
double x = this.getNumerator().doubleValue();
double y = this.getDenominator().doubleValue();
return x / y;
}

@Override
public long longValue() {
return (long) doubleValue();
}

@Override
public int compareTo(Rational o) {
if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) > 0)
return 1;
else if (this.subtract(o).getNumerator().compareTo(BigInteger.ZERO) < 0)
return -1;
else
return 0;
}
}

大家好,我是学java的。我尝试在 Rational 类中使用 BigInteger。上网查了很多地方的代码,还是不行。

如果我在控制台上写输入,程序不会显示任何内容并且不会终止。

我的代码有什么问题?我真的压力很大:(请帮助我!

最佳答案

BigInteger 是不可变的。您不能执行 k.add(BigInteger.ONE) 并期望 k 发生变化。您必须执行k = k.add(BigInteger.ONE)

您可以在构造期间调用的 gcd 方法中执行此操作,因此第一次创建 Rational gcd 时会进入无限循环.

关于java - 我使用 BigInteger 创建了 Rational 类,但没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33958264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com