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

问题描述

大家好,我正在开发软件,我有点需要帮助,您会发现我们需要制定一种可以简化分数的方法.任何想法如何?到目前为止,这是我的代码(不要介意dvalue方法,现在我只需要简化方法即可)

Hey guys I am working on a SW here I am kinda in need of help, you see we need to make a method where we are gonna simplify fractions. any idea how? here's my code as of now (don't mind the dvalue Method it is already finsih all I need is the simplify method)

public class Fraction {

    public int num;
    public int den;
    public double dValue;

    public void display()
    {
        System.out.println("Numerator: "+num);
        System.out.println("Denominator: "+den);
    }

    public double dValue()
    {
        dValue = (double)num/den;
        return dValue;
    }


}


public class FractionTest {

        public static void main(String args[])
        {
            Fraction f = new Fraction();
            f.num = 50;
            f.den = 100;
            f.display();

            double d = f.dValue();
            System.out.println(d);
        }   
}

推荐答案

如果可以遵循以下步骤,则简化分数很容易:

Simplifying fractions is easy if you can folow the steps:

  1. 找到num和den的gcd,所以您有gcd = GCDFind(gcd,num);
  2. 现在,如果gcd == 1,则分数不能简化(它已经是简化形式).
  3. 如果gcd> 1,则newNum = num/gcd;和newDen = den/gcd;

这就是我所需要的.

gcd代表Greates Common Divisor ...代码很容易找到,我只是在几秒钟内用Google搜索JavaScript的工作方式: http://www.calculla.com/en/fraction

The gcd stands for Greates Common Divisor... Code easily findable, and I just googled JavaScript implentation working this way in few seconds: http://www.calculla.com/en/fraction

这篇关于使用Java简化分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 05:26