用Java实现两个具体分数的相加?这代码有逻辑错误,求解答!
韬博 2014-09-07 12:02:36 //分数类
public class FenShu {
private static int fenMu,fenZi;
public FenShu(int m,int n) {
fenZi=m;
fenMu=n;
}
public void addFenShu(int x,int y ){
fenZi=fenZi*y+fenMu*x;
fenMu=fenMu*y;
}
public int getFenZi(){
return fenZi;
}
public int getFenMu(){
return fenMu;
}
public int getGongYueShu(){
if(Math.abs(fenZi)<Math.abs(fenMu))
{
int t=fenZi;
fenZi=fenMu;
fenMu=t;
}
while(fenZi%fenMu!=0)
{
int yuShu=fenZi%fenMu;
fenZi=fenMu;
fenMu=yuShu;
}
return fenMu;
}
}
//主类
public class AppMain {
public static void main(String[] args) {
FenShu fenshu1=new FenShu(3,12);
fenshu1.addFenShu(4, 24);
int gongYueShu=fenshu1.getGongYueShu();
System.out.println("最大公约数为:"+gongYueShu);
int totalFenZi=fenshu1.getFenZi()/gongYueShu;
System.out.println(fenshu1.getFenZi());
int totalFenMu=fenshu1.getFenMu()/gongYueShu;
System.out.println(fenshu1.getFenMu());
System.out.println("3/12 + 4/24="+totalFenZi+"/"+totalFenMu);
}
}
最后运行的结果为:
最大公约数为:24
48
24
3/12 + 4/24=2/1
//很明显错误,求解答工程!谢谢
[size=24px][/size]