0

当我运行我的 main 方法时,我得到以下异常:线程“main”java.lang.StackOverflowError 中的异常,然后是:在 Rational.doubleValue(Rational.java:62) 中出现了 100 多次。这是为我提供主要课程的作业,因此我无法更改其中的任何内容。这是我的代码:


public class Rational extends Number implements Comparable<Rational> 
{
   private int num;
   private int denom; 
   public Rational(int num, int denom)
   {
      this.num = num;
      this.denom = denom;
   }
   public int compareTo(Rational r)
   {
      if ((num/denom) - (r.num/r.denom) > 0)
      {
         return +1;
      }
      else if ((num/denom) - (r.num/r.denom) < 0)
      {
         return -1;
      }
      return 0;
   }

   public Rational add(Rational r)
   {
      return new Rational(num + r.num, denom + r.denom);
   }

   public Rational sub(Rational r)
   {
      return new Rational(num - r.num, denom - r.denom);
   }

   public Rational mul(Rational r)
   {
      return new Rational(num * r.num, denom * r.denom);
   }

   public Rational div(Rational r)
   {
      return new Rational(num * r.denom, denom * r.num);
   }

   public int gcd(Rational r)
   {
      int i = 1;
      while(i != 0)
      {
         i = denom % r.denom;
         denom = r.denom;
         r.denom = i;
      }
      return denom;
   }

   public String toString()
   {
      String a = num + "/" + denom;
      return a;
   }
   public double doubleValue()
   {
      return (double)doubleValue();
   }
   public float floatValue()
   {
      return (float)floatValue();
   }
   public int intValue()
   {
      return (int)intValue();
   }
   public long longValue()
   {
      return (long)longValue();
   }
}

这是主类(我无法修改)


public class Main {

    public static void main(String[] args) {

        Rational a = new Rational(2, 4);
        Rational b = new Rational(2, 6);

        System.out.println(a + " + " + b + " = " + a.add(b));
        System.out.println(a + " - " + b + " = " + a.sub(b));
        System.out.println(a + " * " + b + " = " + a.mul(b));
        System.out.println(a + " / " + b + " = " + a.div(b));

        Rational[] arr = {new Rational(7, 1), new Rational(6, 1),
                          new Rational(5, 1), new Rational(4, 1),
                          new Rational(3, 1), new Rational(2, 1),
                          new Rational(1, 1), new Rational(1, 2),
                          new Rational(1, 3), new Rational(1, 4),
                          new Rational(1, 5), new Rational(1, 6),
                          new Rational(1, 7), new Rational(1, 8),
                          new Rational(1, 9), new Rational(0, 1)};

        selectSort(arr);

        for (int i = 0; i < arr.length-1; ++i) {
            if (arr[i].compareTo(arr[i+1]) > 0) {
                System.exit(1);
            }
        }

//        for (Rational r : arr) {
//            System.out.println(r);
//        }

        Number n = new Rational(3, 2);

        System.out.println(n.doubleValue());
        System.out.println(n.floatValue());
        System.out.println(n.intValue());
        System.out.println(n.longValue());
    }

    public static <T extends Comparable<? super T>> void selectSort(T[] array) {

        T temp;
        int mini;

        for (int i = 0; i < array.length - 1; ++i) {

            mini = i;

            for (int j = i + 1; j < array.length; ++j) {
                if (array[j].compareTo(array[mini]) < 0) {
                    mini = j;
                }
            }

            if (i != mini) {
                temp = array[i];
                array[i] = array[mini];
                array[mini] = temp;
            }
        }
    }
}
4

1 回答 1

2

这些方法

public double doubleValue()
{
   return (double)doubleValue();
}
public float floatValue()
{
   return (float)floatValue();
}
public int intValue()
{
   return (int)intValue();
}
public long longValue()
{
   return (long)longValue();
}

都导致堆栈溢出,因为它们递归地调用自己而没有退出。这不是你想要的。

对于这些方法中的每一个,您都需要编写将您表示的数字转换Rationaldoublefloatint的代码long

顺便说一句,在 , 的定义中还有其他问题,add()但这些问题与您使用的算法相对于适用于这些操作的算术规则有关。subtract()compare()

public class Rational extends Number implements Comparable<Rational> {
    private int num;
    private int denom;

    public Rational(int num, int denom) {
        this.num = num;
        this.denom = denom;
    }

    public int compareTo(Rational r) {
        if ((num / denom) - (r.num / r.denom) > 0) {
            return +1;
        } else if ((num / denom) - (r.num / r.denom) < 0) {
            return -1;
        }
        return 0;
    }

    public Rational add(Rational r) {
        return new Rational(num + r.num, denom + r.denom);
    }

    public Rational sub(Rational r) {
        return new Rational(num - r.num, denom - r.denom);
    }

    public Rational mul(Rational r) {
        return new Rational(num * r.num, denom * r.denom);
    }

    public Rational div(Rational r) {
        return new Rational(num * r.denom, denom * r.num);
    }

    public int gcd(Rational r) {
        int i = 1;
        while (i != 0) {
            i = denom % r.denom;
            denom = r.denom;
            r.denom = i;
        }
        return denom;
    }

    public String toString() {
        String a = num + "/" + denom;
        return a;
    }

    public double doubleValue() {
        return (double)num / (double)denom;
    }

    public float floatValue() {
        return (float)num / (float)denom;
    }

    public int intValue() {
        return Math.round(num / denom);
    }

    public long longValue() {
        return Math.round(num / denom);
    }
}
于 2018-03-22T23:30:28.137 回答