Java 为什么我的while循环被跳过?

Java 为什么我的while循环被跳过?,java,while-loop,Java,While Loop,我认为我的程序跳过了while循环,但老实说,我不确定到底发生了什么。该函数应该通过找到GCD,然后将分子和分母除以该数字来减少分数 class Rational { private int numerator, denominator; //Constructor public Rational (int num, int den) { numerator = num; denominator = den; } //Method for multiplying f

我认为我的程序跳过了while循环,但老实说,我不确定到底发生了什么。该函数应该通过找到GCD,然后将分子和分母除以该数字来减少分数

    class Rational {

private int numerator, denominator;

//Constructor
public Rational (int num, int den) {
    numerator = num;
    denominator = den;
}

//Method for multiplying fractions
public Rational times (Rational that) {
    Rational x = new Rational (this.numerator*that.numerator, this.denominator*that.denominator);
    x = x.reduce();
    return x;
}

//Method for displaying fractions as strings
public String toString() {
    return new String(numerator+"/"+denominator); 
}

//Method for adding fractions
public Rational plus(Rational that) {
    Rational x = new Rational ((this.numerator*that.denominator)+(that.numerator*this.denominator), 
            this.denominator*that.denominator);
    //x = x.reduce();
    return x;
}

//Method for subtracting fractions
public Rational minus(Rational that) {
    Rational x = new Rational ((this.numerator*that.denominator)-(that.numerator*this.denominator),
            this.denominator*that.denominator);
    //x = x.reduce();
    return x;
}

//Method for dividing fractions
public Rational divideBy(Rational that) {
    Rational x = new Rational (this.numerator*that.denominator, this.denominator*that.numerator);
    //x = x.reduce();
    return x; 
}

public Rational reduce() {
    int a = Math.abs(this.numerator);
    int b = Math.abs(this.denominator);
    int c = Math.min(a, b);
    System.out.println(c);
    System.out.println(a%c);
    System.out.println(b%c);
    if (a==0) {
        return new Rational (0,1);
    }
    else {
        while (((a%c)!= 0) && ((b%c)!= 0)) {
            c = c-1;
            System.out.println(c);
        }
        System.out.println(c);
        return new Rational (this.numerator/c,this.denominator/c);
    }
}
}   

public class RationalTester {

public static void main(String[] args) {
    Rational x = new Rational (6,4); //The fraction 6/4
    Rational y = new Rational (5,2); //The fraction 5/2
    Rational z = x.times(y); //Their product
    Rational w = x.plus(y); //Their sum
    Rational v = x.minus(y); //Their difference
    Rational u = x.divideBy(y); //Their quotient
    JOptionPane.showMessageDialog(null, x.toString()+" * "+y.toString()+" = "+z.toString());
    JOptionPane.showMessageDialog(null, x.toString()+" + "+y.toString()+" = "+w.toString());
    JOptionPane.showMessageDialog(null, x.toString()+" - "+y.toString()+" = "+v.toString());
    JOptionPane.showMessageDialog(null, x.toString()+" / "+y.toString()+" = "+u.toString());

}

}

我得到了分子和分母的绝对值,以确保如果分数为负数,我将在最后保持它。如果分子为0,则要求我返回(0,1)。问题是关于while循环。。。似乎它被完全跳过了。有什么建议吗?

因为它的条件总是假的

在第一行中,您将
c
设置为等于
a
b
。因此有两种可能性:

  • 如果
    c==a
    ,则
    a%c
    将为零。所以while条件是假的
  • 如果
    c==b
    ,则
    b%c
    将为零。所以while条件是假的

  • 因为它的条件总是错误的

    在第一行中,您将
    c
    设置为等于
    a
    b
    。因此有两种可能性:

    • 如果
      c==a
      ,则
      a%c
      将为零。所以while条件是假的
    • 如果
      c==b
      ,则
      b%c
      将为零。所以while条件是假的

    提供一些用于测试的样本输入和所需输出。请给出
    a
    b
    c
    的样本值。。。请注意,比较浮点数是否相等很少是一个好主意。它们是否需要是
    double
    而不是
    int
    ?请注意,如果您提供一个简短但完整的程序来演示问题,这会有所帮助……对不起,这是完整的程序。它最初是int而不是double,我只是尝试了很多不同的方法来获得期望的结果,我把它改回来了。提供一些样本输入和期望的输出以进行测试请给出
    a
    b
    c
    的样本值。。。请注意,比较浮点数是否相等很少是一个好主意。它们是否需要是
    double
    而不是
    int
    ?请注意,如果您提供一个简短但完整的程序来演示问题,这会有所帮助……对不起,这是完整的程序。它原来是int而不是double,我只是尝试了很多不同的方法来得到想要的结果,我把它改回来了。这就是原因。你说c=min(a,b),所以a%c==0或b%c==0总是!哈哈哈,非常感谢你!这是其中之一,我的头撞在墙上的时间太长了。伙计,我觉得自己很笨,这就是原因。你说c=min(a,b),所以a%c==0或b%c==0总是!哈哈哈,非常感谢你!这是其中之一,我的头撞在墙上的时间太长了。伙计,我觉得自己很笨。