Java程序操纵数字意外错误

时间:2017-04-06 01:39:47

标签: java

这个程序是我练习创建对象和定义类。我有多个方法用理性类编写,而我的主要用主类编写。所有的方法都在main中正确调用,但是当我调用add和reduce方法时,我收到的错误是无法找到方法。我已经在下面的行中评论了我收到错误的地方。

这是我理性的课程:

package main;


public class rational {
    int numerator, denominator;
    public rational() {
        this.numerator = 0;
        this.denominator = 0;

    }

    public static void printRational(rational tester) {
        System.out.println(tester.numerator +  "/"  + tester.denominator);
    }

    public rational(int n, int d) {
        this.numerator = n;
        this.denominator = d;
    }

    public static void negate(rational tester) {
        tester.numerator = tester.numerator * -1;
        if(tester.denominator <= 0) {
            tester.numerator = tester.numerator * -1;
            tester.denominator = tester.denominator * -1;}
        }
        public static void invert(rational tester) {
            int tempN = tester.numerator;
            int tempD = tester.denominator;

            tester.numerator = tempD;
            tester.denominator = tempN;
        }
        public static double toDouble(rational tester) {
            double retval = (double)tester.numerator / (double)tester.denominator;
            return retval;
        }

        public static void reduce(rational tester) {
            int remain, num1, num2;
            num1 = tester.numerator;
            num2 = tester.denominator;
            remain = num1 % num2;
            while(remain != 0) {
                remain = num1 % num2 ;
                num1 = num2;
                num2 = remain;
            }
            int GCD = num1;

            rational ret = new rational(tester.numerator/GCD, tester.denominator/GCD);

            printRational(ret);
        }

        //Step 11  Write a method called add that takes two rational numbers as arguments and returns a new rational object. The return object should contain the sum of the arguments.
        public static rational add(rational tester, rational testAdd) {
            rational retAdd = new rational(0, 0);
            if(tester.denominator == testAdd.denominator) {
                retAdd.numerator = tester.numerator + testAdd.numerator;
                retAdd.denominator = tester.denominator;
                return retAdd;
            } else {
                retAdd.numerator = tester.numerator * testAdd.denominator;
                retAdd.denominator = tester.denominator * testAdd.denominator;

                testAdd.numerator = testAdd.numerator * tester.denominator;
                testAdd.denominator = testAdd.denominator * tester.denominator;

                retAdd.numerator += testAdd.numerator;

                int remain, num1, num2;
                num1 = retAdd.numerator;
                num2 = retAdd.denominator;
                remain = num1 % num2;
                while(remain != 0) {
                    remain = num1 % num2 ;
                    num1 = num2;
                    num2 = remain;
                }
                int GCD = num1;

                retAdd.numerator = retAdd.numerator/GCD;
                retAdd.denominator = retAdd.denominator/GCD;

                return retAdd;
            }
        }
    }
}

这是我在不同文件中的主要类:

package main;

import static main.rational.toDouble;

public class Main {

    public static void main(String[] args) {

        rational tester = new rational();
        tester.numerator=4;
        tester.denominator=3;

        rational testAdd = new rational(5, 7);    

        rational.printRational(tester);

        rational.negate(tester);
        rational.printRational(tester);

        rational.invert(tester);
        rational.printRational(tester);

        double doubleValue = toDouble(tester);
        System.out.println(doubleValue);

        reduce(tester);   //ERROR HERE

        printRational(add(tester, testAdd));    //ERROR HERE
    }
}

2 个答案:

答案 0 :(得分:1)

您正在从toDouble静态导入rational方法(BTW,Java类traditionally是大写的,因此您应该将您的类命名为Rational),但不能其他的,如reduce,所以你不能在没有用类名称添加它们的情况下调用它们。

所以rational.reduce(tester);会有效,将import static main.rational.reduce;添加到Main类的顶部也是有效的。

答案 1 :(得分:0)

只导入'static main.rational.toDouble'int class Main这个方法不正确;你可以把它改成import static com.syoka.test1.rational。*;