在Java中减去负分数的问题

时间:2014-03-11 04:44:51

标签: java

出于某种原因,我得到了正确的结果,但我的负号是有问题的。 例如,如果我做1/4 - (-2/4),我得到(-3/4)。

这是减去分数的减法。

   /**
   Subtracts a fraction from another fraction.
   @param toUse, the fraction to subtract.
   @return minusFraction, the result after subtraction.
   */   
   public Fraction minus(Fraction toUse)
   {
      int newNum = ((toUse.numerator * denominator)
                     - (numerator * toUse.denominator));
      int newDen = denominator * toUse.denominator;
      Fraction minusFraction = new Fraction(newNum, newDen);

      return minusFraction;
   }

这是我的reduce()方法,以防万一...

  /**
   Reduces the fraction, if possible, to it's simplest form.
   Converts negative fractions to the form -x/y, or if -x/-y --> x/y
   */   
   private void reduce()
   {
      int lowest = Math.abs(numerator);
      int highest = Math.abs(denominator);

      if (lowest > highest)
      {
         int temp = highest;
         highest = lowest;
         lowest = temp;
      }

      while (lowest != 0)
      {
         int temp = lowest;
         lowest = highest % lowest;
         highest = temp;
      }

      numerator /= highest;
      denominator /= highest;

      if (denominator < 0)
      {
         numerator *= -1;
         denominator *= -1;
      }
   }

我只是从我之前添加的方法中切换了一个操作符,这里也给出了。我认为只将+转换为 - 可能会导致我的问题。

   /**
   Adds two fractions together.
   @param toUse, the fraction to be added.
   @return plusFraction, the sum of the two fractions.
   */   
   public Fraction plus(Fraction toUse)
   {
      int newNum = ((toUse.numerator * denominator)
                     + (numerator * toUse.denominator));
      int newDen = denominator * toUse.denominator;
      Fraction plusFraction = new Fraction(newNum, newDen);

      return plusFraction;
   }

2 个答案:

答案 0 :(得分:1)

我认为问题出在你的Fraction Minus功能中:

public Fraction minus(Fraction toUse)
{
  int newNum = ( (numerator * toUse.denominator)-
                       (toUse.numerator * denominator)
                 );
  int newDen = denominator * toUse.denominator;
  Fraction minusFraction = new Fraction(newNum, newDen);

  return minusFraction;
}

例如: 1/4 - ( - 2/4)..... 这里的toUse.numerator是-2而toUseDenominator是4 您的代码正在做什么使您的使用分数(-2/4)作为基本分数,并从原始基础分数(1/4)减去,即(-2/4) - (1/4)...因此结果-3/4

希望它有效

答案 1 :(得分:0)

两个问题:

  1. 您正在以错误的顺序减去。

    你的javadoc说:

    /**
     Subtracts a fraction from another fraction.
     @param toUse, the fraction to subtract.
     @return minusFraction, the result after subtraction.
     */ 
    

    这意味着this.numerator - toUse.numerator

    但在这里你做了相反的事情:

    int newNum = ((toUse.numerator * denominator)
                 - (numerator * toUse.denominator));
    

    当你致电1/4.minus(-2/4)时,最终会计算:

    int newNum = (-2*4) - (1*4) = -9
    int newDen = 4 * -4 = -16
    // Your new fraction will have -9/-16 which you reduce to -3/-4 
    // (which is how you hold -3/4)
    

    只需将代码切换为:

    /**
    Subtracts a fraction from another fraction.
    @param toUse, the fraction to subtract.
    @return minusFraction, the result after subtraction.
    */   
    public Fraction minus(Fraction toUse)
    {
        int newNum = ((numerator * toUse.denominator)
                 - (toUse.numerator * denominator));
        int newDen = denominator * toUse.denominator;
        Fraction minusFraction = new Fraction(newNum, newDen);
    
        return minusFraction;
    }
    

    现在何时

    int newNum = (1*4) - (-2*4) = 9
    int newDen = 4 * -4 = -16
    
  2. 这给我们带来了第二个问题。您的reduce方法依赖于分母来确定数字是负数还是正数。但是分子应该真的带有符号,因为你在分子上做了减法。