gcd公约数 [英] gcd common divisor

查看:63
本文介绍了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();


}


}



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

解决方案

首先,您可以尝试从现在开始将代码放入代码标签中(当您'时,请查看右侧的框)重新发布说REPLY GUIDELINES,谢谢。它使代码更容易阅读。


接下来,究竟是什么问题?当编译器出现什么错误尝试运行这个程序?


-blazed


我想知道什么是最好的方法,但是我错过了一些关于命名的东西,因为所有我的编译错误告诉我,我有不兼容的类型



我想知道什么是最好的方法,但我错过了一些关于命名,因为我的所有编译错误告诉我,我有不兼容的类型



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.

解决方案

Firstly, could you please try to put your code in code tags from now on (look at the box on the right when you''re posting that says "REPLY GUIDELINES", thank you. It makes the code much easier to read.

Next, what exactly is the problem? What error is showing up when the compiler tries to run this program?

-blazed


I wonder what is the best method, but I''m missing something about naming because all my compiling errors are telling me that I have incompatible types


I wonder what is the best method, but I''m missing something about naming because all my compiling errors are telling me that I have incompatible types

mod is for % ?


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆