简体   繁体   English

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

[英]Incorrect output in fraction equality program

I'm a beginner java programmer writing a program to test the equality of two user-inputted fractions. 我是一名Java初学者,正在编写程序来测试两个用户输入的分数是否相等。 There's a Fraction class that includes: mutator methods for setting the numerators and denominators, a method for displaying the fraction as a ratio, and a method for testing equality. 有一个Fraction类,其中包括:用于设置分子和分母的mutator方法,用于将分数显示为比率的方法以及用于测试相等性的方法。 There's also a Main class that allows the user to create fractions. 还有一个Main类,允许用户创建分数。 I'm having trouble getting the correct output. 我无法获得正确的输出。 It appears no matter the input, the fractions are always "NOT equal". 无论输入如何,分数总是“不相等”。 Here is an example: 这是一个例子:

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.

Here is the code for the class Fraction: 这是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;
    }
}

Here is the code for the class Main : 这是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();
            }
    }
}

The equals() method presented in your code for class Fraction is not the same one being invoked by Main.main() . 代码中针对类Fractionequals()方法与Main.main()调用的方法Main.main() You provide Fraction.equals(int, int, int, int) , which would make more sense as a static method, but main() invokes the equals(Object) method that Fraction inherits from Object . 您提供Fraction.equals(int, int, int, int) ,作为static方法更有意义,但是main()调用FractionObject继承的equals(Object)方法。

If you want to override Object 's method, then your method must have the same parameter type(s). 如果要覆盖Object的方法,则您的方法必须具有相同的参数类型。

Update: Note also that the code for your equals(int, int, int, int) method is also erroneous, as @nhouser9 observed in his now-deleted answer. 更新:还请注意, equals(int, int, int, int)方法的代码也是错误的,正如@ n​​houser9在他现在删除的答案中观察到的那样。 You are performing integer divisions to try to compute floating-point quotients for comparison, and this will give you many false positives. 您正在执行整数除法,以尝试计算浮点商以进行比较,这将给您带来许多误报。

Really, it's questionable to use floating-point at all, because most fractions are not exactly representable as double s. 确实,完全使用浮点数是有问题的,因为大多数分数不能精确地表示为double It would be better to test for equality by cross-multiplying: 最好通过交叉相乘测试是否相等:

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

If you do that then do not neglect the casts to long , for otherwise some possible inputs will cause overflow. 如果你这样做的话就不要忽略了强制类型转换来long ,否则一些可能的输入将造成溢出。

Alternatively, you could reduce both fractions to their simplest terms, and compare that way. 或者,您可以将两个分数都简化为最简单的术语,然后进行比较。 Presumably you would use Euclid's method to compute the GCDs needed for the reductions. 大概您将使用Euclid方法计算减少量所需的GCD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM