分数相等程序中的输出不正确

时间:2016-05-01 01:54:19

标签: java

我是一名初学Java程序员,编写程序来测试两个用户输入分数的相等性。有一个Fraction类,包括:用于设置分子和分母的mutator方法,用于显示分数作为比率的方法,以及用于测试相等性的方法。还有一个Main类允许用户创建分数。我无法获得正确的输出。无论输入如何,分数总是“不相等”。这是一个例子:

This application allows you to test if two fractions are equal to each other.
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 2
The fraction you entered is: 1/2
Enter the numerator of the target fraction: 2
Enter the denominator of the target fraction: 4
The target fraction you entered is: 2/4
The two fractions being compared are 1/2 and 2/4...
The two fractions are NOT equal.
Would you like to test another fraction? Enter 0 to quit anything else to continue.

以下是Fraction类的代码:

public class Fraction {

    int numerator1;
    int denominator1;
    int numerator2;
    int denominator2;

    public void Fraction() {
        numerator1 = 0; //initialize variables
        denominator1 = 1;
        numerator2 = 0;
        denominator2 = 1;
    }

    public void setNum1(int num) { //mutator method
        numerator1 = num;
    }

    public void setDen1(int denom) { //mutator method
        denominator1 = denom;
    }

    public void setNum2(int num) {
        numerator2 = num;
    }

    public void setDen2(int denom) {
        denominator2 = denom;
    }

    public boolean equals(int numerator1, int numerator2, int denominator1, int denominator2) {
        double frac1;
        double frac2;
        frac1 = numerator1/denominator1;
        frac2 = numerator2/denominator2;

        if(frac1 == frac2)
            return true;
        else
            return false;
    }

    public String displayFraction1() {
        String ratio1;
        ratio1 = (numerator1 + "/" + denominator1);
        return ratio1;
    }

    public String displayFraction2() {
        String ratio2;
        ratio2 = (numerator2 + "/" + denominator2);
        return ratio2;
    }
}

以下是Main类的代码:

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Fraction create = new Fraction(); //creating fraction objects
        Fraction target = new Fraction();
        int num1, num2; //stores numerators of 2 fractions
        int den1, den2; //stores denominators of 2 fractions
        int input = 1; //initialized to 1 for while loop
        Scanner keyboard = new Scanner(System.in); 

        System.out.println("This application allows you to test if two fractions are equal to each other.");
        System.out.print("Enter the numerator of the first fraction: ");
        num1 = keyboard.nextInt();
        create.setNum1(num1);
        System.out.print("Enter the denominator of the first fraction: ");
        den1 = keyboard.nextInt();
        create.setDen1(den1);
        System.out.println("The fraction you entered is: " + create.displayFraction1());

        while(input != 0) { //allows user to hold initial fraction and repeatedly tests against with target fractions
            System.out.print("Enter the numerator of the target fraction: ");
            num2 = keyboard.nextInt();
            target.setNum2(num2);
            System.out.print("Enter the denominator of the target fraction: ");
            den2 = keyboard.nextInt();
            target.setDen2(den2);
            System.out.println("The target fraction you entered is: " + target.displayFraction2());
            System.out.println("The two fractions being compared are " + create.displayFraction1() + " and " + target.displayFraction2() + "...");      

            if(create.equals(target)) //calls equals method to test equality
                System.out.println("The two fractions are equal.");
            else
                System.out.println("The two fractions are NOT equal.");

            System.out.println("Would you like to test another fraction? Enter 0 to quit anything else to continue.");
            input = keyboard.nextInt();
            }
    }
}

1 个答案:

答案 0 :(得分:6)

您的代码中为equals()提供的Fraction方法与Main.main()调用的方法不同。您提供的Fraction.equals(int, int, int, int)作为static方法更有意义,但main()会调用equals(Object)Fraction继承的Object方法。

如果要覆盖Object的方法,则您的方法必须具有相同的参数类型。

<强>更新 另请注意,equals(int, int, int, int)方法的代码也是错误的,正如@nhouser9在他现在删除的答案中观察到的那样。您正在执行整数除法以尝试计算浮点商以进行比较,这会给您带来许多误报。

实际上,完全使用浮点值得怀疑,因为大多数分数并不能完全表示为double s。最好通过交叉乘法来测试相等性:

return (long) numerator1 * denominator2 == (long) numerator2 * denominator1;

如果你这样做,那么不要忽视对long的强制转换,否则一些可能的输入会导致溢出。

或者,您可以将两个分数减少到最简单的术语,并进行比较。大概你会使用欧几里德的方法来计算减少所需的GCD。