java 如何在Java中添加分数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26812131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:39:50  来源:igfitidea点击:

How to add fractions in Java

javamathfractions

提问by user2647699

I am trying to make a simple program that adds two fractions.

我正在尝试制作一个添加两个分数的简单程序。

    public static void add(int nom1, int denom1, int nom2, int denom2){

    int comd = denom1*denom2; //creates common denominator by multiplying both denominators

    int answer = nom1*nom2+nom1*nom2; //attempting to get answer (not working very well)

    System.out.println(nom1+"/"+denom1+" + "+nom2+"/"+denom2+" = "+answer+"/"+comd); // outputs   test like 1/2 + 4/8 = 8/8

    start(); // restarts the program
}

Unfortunately, my calculation to find the sum doesnt work, and i can not figure out another way of doing this. Thanks in advance for anyone who helps. Sorry if this seems noobish, but I can't find anything online that works that I can understand.

不幸的是,我找到总和的计算不起作用,我想不出另一种方法来做到这一点。预先感谢任何提供帮助的人。对不起,如果这看起来很笨拙,但我在网上找不到任何我能理解的东西。

回答by jrahhali

I'm pretty sure it's supposed to be:

我很确定它应该是:

int answer = nom1*denom2 + nom2*denom1;

instead of:

代替:

int answer = nom1*nom2+nom1*nom2;

回答by user8786264

Try this

试试这个

public static void add(int nom1, int denom1, int nom2, int denom2){

    int comd = denom1*denom2; //creates common denominator by multiplying both denominators

    int newNomAnswer = nom1*denom2 + nom2*denom1;


    System.out.println(nom1 + "/" + denom1 + " + " + nom2 + "/" + denom2 + " = " + newNomAnswer + "/" + comd);

    start(); 
}

回答by FunctionR

Although the answer by jrahhali is correct. My version scales nicely if you chose to add more than 2 fraction.

尽管 jrahhali 的答案是正确的。如果您选择添加 2 个以上的分数,我的版本可以很好地扩展。

 int comd = denom1 * denom2;
 int answer = (comd/denom1 * nom1) + (comd/denom2 * nom2);

 System.out.println(nom1 + "/" + denom1 + " + " + nom2 + "/" + denom2 
                    + " = " + answer + "/" + comd);

This is because I don't skip the division like he does.

这是因为我不像他那样跳过分区。

回答by Martin Johansen

The way to add two fractions in Java is to first find the Least Common Multiple (LCM)of the two denominators: denom = lcm(denom1, denom2). This is the denominator of the answer.

Java中两个分数相加的方法是先求两个分母的最小公倍数(LCM):denom = lcm(denom1, denom2)。这是答案的分母。

Then you find the nominator using the follwing formula: nom = denom/denom1*nom1 + denom/denom2*nom2.

然后您可以使用以下公式找到提名者:nom = denom/denom1*nom1 + denom/denom2*nom2。

Finally, you can simplify the fraction by dividing both the numerator and denomerator by their Greatest Common Divisor (GCD). Try it out with this online widget.

最后,您可以通过将分子和分母除以它们的最大公约数 (GCD)来简化分数。试试这个在线小部件

回答by Code giver

What I did was instead of saying nom and denom every time, I say commDeno and totNum. Code:

我所做的不是每次都说 nom 和 denom,而是说 commDeno 和 totNum。代码:

Public void AddFraction(int nom, int denom, int nom2, int denom2){

  Int comD, totNom;

  Nom = nom*denom2;
  Nom2 = nom2*denom;
  totNom =num+num2;
  comD = denom*denom2;
  Sysout(totNum + "/" +comD);

}