用对象划分分数

时间:2019-03-06 03:46:55

标签: java fractions

我有一个班级选手,应该做分数时保持它的变化,然后翻转它。

public class Fraction2
{
private int numerator = 0;
private int denominator = 1;
public Fraction2()
{
    numerator = 0;
    denominator = 1;
}
public Fraction2(int num, int denom)
{
    numerator = num;
    denominator = denom;
}

public void show(int num, int denom)
{
    System.out.println(numerator + "/" + denominator);
}
public String toString()
{
    return (numerator + "/" + denominator);
}

public Fraction2 mult(Fraction2 temp)
{
    return new Fraction2(this.numerator * temp.numerator,
                         this.denominator * temp.denominator);
}
public Fraction2 add(Fraction2 sent)
{
   int cmmndenom = sent.denominator * this.denominator;
   int answer = this.numerator * sent.denominator + sent.numerator * this.denominator;
   return new Fraction2(answer, cmmndenom);
}
public Fraction2 divide(Fraction2 sent)
{
    //keep it change it flip it method
    Fraction2 answer = new Fraction2(this.numerator * sent.denominator,
                                     this.denominator * sent.numerator);
    return answer;
}
public Fraction2 reduce()
{
    int ansNumer = numerator;
    int ansDenom = denominator;
    for (int i = 1; ((i <= (numerator)) && i <= (denominator)); i++)
    {
        if (((numerator % i) == 0)  && ((denominator % i) == 0))
        {
            ansNumer = numerator / i;
            ansDenom = denominator / i;
        }
    }
    return new Fraction2(ansNumer, ansDenom);
}

}

当我输入1/4和2/6时,我应该得到6/8,但我得到8/6。除分割部分外,其余方法均有效。我只是停留在如何获得6/8而不是8/6上。

import java.util.Scanner;
public class FractionTesterv2
{
    public static void main(String args[])
    {
        System.out.println("\f");
        Scanner input = new Scanner(System.in);
        int numerator;
        int denominator;
        int addCount = 0;
        int multCount = 0;
        int divCount = 0;
        //***************************************
        //Creating future objects
        Fraction2 fraction3 = new Fraction2();
    Fraction2 addAnswer = new Fraction2();
    Fraction2 multAnswer = new Fraction2();
    Fraction2 divAnswer  = new Fraction2();
    //***************************************
    System.out.println("Enter a numerator");
    numerator = input.nextInt();
    System.out.println("Enter a denominator");
    denominator = input.nextInt();
    //creates first fraction
    Fraction2 fraction2 = new Fraction2(numerator, denominator);
    System.out.println("would you like to add another fraction?");
    String choice = input.nextLine();
    choice = input.nextLine();
    // Will only display first fraction entered
    if (choice.equals("no"))
        System.out.println(fraction2);
    else
    {
        while  (!(choice.equals("no")))
        {
            System.out.println("Enter a numerator");
            numerator = input.nextInt();
            System.out.println("Enter a denominator");
            denominator = input.nextInt();
            fraction3 = new Fraction2(numerator, denominator);
            //***************************************
            //Adding Fractions
            if (addCount == 0)
                addAnswer = fraction2;
            addCount = 1;
            addAnswer = fraction3.add(addAnswer);
            System.out.println("add: " + addAnswer);
            //***************************************
            //Multiplying Fractions
            if (multCount == 0)
                 multAnswer = fraction2;
            multCount = 1;
            multAnswer = fraction3.mult(multAnswer);
            System.out.println("Multiply: " + multAnswer);
            //***************************************
            //Dividing Fractions
            if (divCount == 0)
                divAnswer = fraction2;
            divCount = 1;
            divAnswer = fraction3.divide(divAnswer);
            System.out.println("Divide: " + divAnswer);
            //***************************************
            System.out.println("would you like to add another Fraction?");
            choice = input.nextLine();
            choice = input.nextLine();
        }
        Fraction2 addReduced = addAnswer.reduce();
        Fraction2 multReduced = multAnswer.reduce();
        Fraction2 divReduced = divAnswer.reduce();
        System.out.println("Adding Reduced: " + addReduced);
        System.out.println("Multiply Reduced: " + multReduced);
        System.out.println("Dividing Reduced: " + divReduced);
    }            
}

}

我的代码相当大,因此我可以确定如何简化它。一方面只是寻求帮助。

1 个答案:

答案 0 :(得分:0)

您输入的前两个数字(“ 1”,“ 4”)用于构建fraction2

接下来的两个数字(“ 2”,“ 6”)用于构建fraction3

然后您进行计算:

divAnswer = fraction3.divide(fraction2);

“ 2/6”除以“ 1/4”是“ 8/6”-如果要“ 6/8”,则必须计算“ 1/4”除以“ 2/6”,这会是

divAnswer = fraction2.divide(fraction3);

对于链式除法运算,您可以编写

// this shows an extract of a larger fraction of your code!
divAnswer = fraction2;
while  (!(choice.equals("no")))
    //...
    // no need for some strange divCount flag here
    divAnwser = divAnswer.divide(fraction3);
    //...
}

或者,类似于您现在所做的事情:

while  (!(choice.equals("no")))
    //...
    if (divCount == 0)
        divAnswer = fraction2;
    divCount = 1;
    divAnswer = divAnswer.divide(fraction3);
    //...
}

基本问题是您当前的代码交换了除法运算,这就是我试图显示的内容。