本文介绍了gcd公约数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我正在寻找关于这个特定问题的一些帮助。


我需要创建一个可以简化分数的方法,即表示它是分子和分母没有公约数的分数。它应该在分子和分母中读取,然后以简化形式打印出分数。


我试着计算它:

而n! = 0

{

r = m mod n;

m = n;

n = r;

}

返回m


我试图实现这个,但我在java中遇到问题


class Fraction {


private int numerator;


private int denominator;


public Fraction(int num,int denom){


setNumerator(num);


setDenominator(denom);


}


public int getDenominator(){


return分母;


}


public int getNumerator(){


返回分子;


}


public void setDenominator(int denom){


if(denom == 0) {$ / $

System.err.println(致命错误);


System.exit(1); //终止节目


}


分母= denom;


}



public void setNumerator(int num){


numerator = num;


}


public String toString(){


返回getNumerator()+" /" + getDenominator();


}


}



谢谢你提前寻求任何帮助。

解决方案





mod for% ?

I''m new to Java and I was looking for some help on this particular problem.

I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.

I tried to calculate it with:
while n!=0
{
r = m mod n;
m = n;
n = r;
}
return m

I tried to implement this, but i''m having problem with it in java

class Fraction{

private int numerator;

private int denominator;

public Fraction(int num, int denom){

setNumerator(num);

setDenominator(denom);

}

public int getDenominator(){

return denominator;

}

public int getNumerator(){

return numerator;

}

public void setDenominator(int denom){

if(denom == 0){

System.err.println("Fatal Error");

System.exit(1); //terminate program

}

denominator = denom;

}



public void setNumerator(int num){

numerator = num;

}

public String toString(){

return getNumerator() + "/" + getDenominator();

}

}


Thank you in advance for any help.

解决方案



mod is for % ?


这篇关于gcd公约数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:54