【问题标题】:While loop not executingWhile循环未执行
【发布时间】:2015-07-18 17:57:33
【问题描述】:

我是 java 新手,需要帮助。我的 while 循环不起作用。它除了 count 不执行任何操作。

对于这个问题,我需要创建具有 mutator 和辅助方法的分数类。然后我必须将分子和分母设为最低形式,所以我使用 while 循环通过使用模块找到最大公约数。任何其他评论家也受到赞赏。

public class Fraction {
    private int numerator;
    private int denominator;
    public Fraction(){

    }
    public Fraction(int num, int den){
        this.numerator = num;
        this.denominator = den;
    }

    public void setNumerator(int numerator){
        this.numerator = numerator;
    }
    public void setDenominator(int denominator){
        this.denominator = denominator;
    }
    public double divided(){
        double divide = (double)this.numerator/this.denominator;
        return divide;
    }
    public String printFraction(){
        int numerator = greatestCommmonNumetaror();
        return "";
    }
    private int greatestCommmonNumetaror(){
        int count = 1;
        int first;
        int mod;
        while(count != this.numerator){// need help with this
            mod = this.numerator%count;
            if(mod != 0){
                first = count;
                if(first < count){
                    first = count;
                }
            }
            count++;
        }
        return 1;
    }
}

【问题讨论】:

  • 你说的“除了计数什么都不执行”是什么意思。
  • 输入一些printlns,这样你就可以确定正在运行的是什么。
  • 我不确定您的 while 循环试图实现什么。你设置了一堆变量,但似乎没有使用它们,最后你只返回“1”。你能用语言描述一下这个算法想要做什么吗?
  • 第一个 = 计数;如果(第一个
  • 请说明您认为您的循环在做什么,以及您认为它应该做什么。

标签: java eclipse debugging while-loop greatest-common-divisor


【解决方案1】:

如果你调用它,循环会很好地执行。 刚刚添加了几个 System.out.println,你可以看到它正在执行:

private int greatestCommmonNumetaror(){
    int count = 1;
    int first;
    int mod;
    System.out.println("Outside while: count is "+count+" and this.numerator is "+this.numerator);
    while(count != this.numerator){// need help with this
        System.out.println("In while: Count is "+count);
        mod = this.numerator%count;
        System.out.println("In while: mod is "+mod);
        if(mod != 0){
            first = count;
            System.out.println("In while and if: first is "+first+" and count is "+count);
            if(first < count){
                first = count;
                System.out.println("In while and if and if: first is "+first+" and count is "+count);
            }
        }
        count++;
    }
    return 1;
}

public String printFraction(){
    int numerator = greatestCommmonNumetaror();
    return "";
}

还添加了下面提到的代码来调用 printFraction:

public static void main(String[] args){
    Fraction fraction = new Fraction(3,5);
    fraction.printFraction();
}

你可以看到输出:

Outside while: count is 1 and this.numerator is 3
In while: Count is 1
In while: mod is 0
In while: Count is 2
In while: mod is 1
In while and if: first is 2 and count is 2

所以它确实进入了while循环。

*编辑:虽然使用 System.out.println 你应该能够看到你的算法的问题,但如果你有任何进一步的问题,你能用例子解释你的算法吗?

【讨论】:

    【解决方案2】:

    只需使用 numerator 摆脱循环中的 this.numerator

    【讨论】:

    • 我不是 dvter,但你的回答绝对不能解决他的问题。
    • this.xx 不同的唯一情况是,如果您在函数/构造函数中同时具有参数x,并且您的对象还有一个名为x 的变量。在这种情况下,您可以使用this.x 来区分提供的参数和对象的实际成员。
    • 是的,但如果没有执行语句,则在该循环中只执行计数