If和Else If语句不触发

时间:2020-02-28 23:04:56

标签: java

所以我正在做学校作业,但我不知道为什么我的程序无法正常工作。此方法旨在比较分数类中两个分数的值。

无论如何,该方法返回0,而我一生都无法找出原因。方法如下:

public int compareTo(Fraction other) {   
    int value;
    int newDenom = other.getDenom();

    if (denominator == newDenom) {
        if (numerator > other.getNum()) {
            value = 1;
        }
        else if (numerator < other.getNum()) {
            value = -1;
        }
        else {
            value = 0;
        }
    }
    else {
        numerator *= newDenom;
        int newNum = other.getNum() * denominator;

        if (numerator > newNum) {
            value = 1;
        }
        else if (numerator < newNum) {
            value = -1;
        }
        else {
            value = 0;
        }
    }        
    return value;
}

感谢所有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

问题出在您后面的else部分。不用使分母相等(例如d)然后将numeratord/denominator相乘,而只是将numeratornewDenom相乘。

else {
    numerator *= newDenom;
    int newNum = other.getNum() * denominator;

    if (numerator > newNum) {
        value = 1;
    }
    else if (numerator < newNum) {
        value = -1;
    }
    else {
        value = 0;
    }
}

正确的做法如下:

class MyMathUtil {
    public static int gcd(int a, int b) {
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }

    public static int lcm(int a, int b) {
        return (a * b) / gcd(a, b);
    }
}

class Fraction implements Comparable<Fraction> {
    private int numerator;
    private int denominator;

    public Fraction(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public int getNumerator() {
        return numerator;
    }

    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public void setDenominator(int denominator) {
        this.denominator = denominator;
    }

    @Override
    public int compareTo(Fraction other) {
        int lcmDenominators = MyMathUtil.lcm(denominator, other.getDenominator());
        Integer numerator1 = numerator * (lcmDenominators / denominator);
        Integer numerator2 = other.getNumerator() * (lcmDenominators / other.getDenominator());
        return numerator1.compareTo(numerator2);
    }

    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }
}

public class Main {
    public static void main(String[] args) {
        // Tests
        Fraction f1 = new Fraction(10, 25);
        Fraction f2 = new Fraction(10, 40);
        Fraction f3 = new Fraction(10, 15);
        Fraction f4 = new Fraction(2, 5);
        compareAndPrint(f1, f2);
        compareAndPrint(f1, f3);
        compareAndPrint(f1, f4);
    }

    static void compareAndPrint(Fraction first, Fraction second) {
        System.out.println(first.compareTo(second) == 0 ? (first + " = " + second)
                : (first.compareTo(second) > 0 ? (first + " > " + second) : (first + " < " + second)));
    }
}

输出:

10/25 > 10/40
10/25 < 10/15
10/25 = 2/5