while循环不执行
While loop not executing
我是 java 的新手,需要帮助。我的 while 循环不工作。它不执行除计数以外的任何操作。
对于这个问题,我需要制作具有增变器和辅助方法的分数 class。然后我必须将分子和分母设为最低形式,所以我使用 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;
}
}
只需使用分子去掉循环中的this.numerator
如果调用 While 循环,它会很好地执行。
刚刚添加了几个 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 您应该能够看到您的算法存在问题,但如果您还有任何其他问题,能否请您举例说明您的算法。
我是 java 的新手,需要帮助。我的 while 循环不工作。它不执行除计数以外的任何操作。
对于这个问题,我需要制作具有增变器和辅助方法的分数 class。然后我必须将分子和分母设为最低形式,所以我使用 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;
}
}
只需使用分子去掉循环中的this.numerator
如果调用 While 循环,它会很好地执行。 刚刚添加了几个 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 您应该能够看到您的算法存在问题,但如果您还有任何其他问题,能否请您举例说明您的算法。