我试图以这样一种方式编写TestFraction,即主方法调用方法getFraction1getFraction2来创建两个Fraction:fr1(a / b)和fr2(a / b)。 getFraction1getFraction2提示用户输入两个整数,分别为ab,并调用getNumber捕获这些整数。分数然后对fr1和fr2进行计算。问题是,当我运行TestFraction时,a和b的值保留为1,这是它们在小数中设置的值。谁能给我一个提示,为什么getFraction1和getFraction2实际上没有将a和b的值传递给fr1和fr2?

这是我的TestFraction代码:

package Fraction;

import java.io.*;
import java.util.*;

public class TestFraction
{

    static Scanner console = new Scanner (System.in);

    private static int getNumber() throws InputMismatchException
    {
                    return console.nextInt();
    }

    private static Fraction getFraction1()
    {
            int a=1, b=1;
            Fraction frac = new Fraction ();

            while (true) {

                    // prompt to enter a numerator and a denominator
                    System.out.println("Input 1st fraction numerator and denominator");
                    // input the numerator and the denominator using getNumber()
                    try {
                            a = getNumber();
                            b = getNumber();
                            }
                    catch (Exception e)
                    {
                            System.out.println("Exception: "+e.toString());
                            console.nextLine();
                            continue;
                    }
                    return frac;
                    // return new Fraction if OK
                    // otherwise print an error message
            }
    }

    private static Fraction getFraction2()
    {
            int a=1, b=1;
            Fraction frac = new Fraction ();

            while (true) {

                    // prompt to enter a numerator and a denominator
                    System.out.println("Input 2nd fraction numerator and denominator");
                    // input the numerator and the denominator using getNumber()
                    try {
                            a = getNumber();
                            b = getNumber();
                    }

                    catch (Exception e)
                    {
                            System.out.println("Exception: "+e.toString());
                            console.nextLine();
                            continue;
                    }
                    return frac;
                    // return new Fraction if OK
                    // otherwise print an error message
            }
    }
    public static void main(String[] args)
    {
            Fraction fr1 = new Fraction ();
            fr1 = getFraction1();

            Fraction fr2 = new Fraction ();
            fr2 = getFraction2();

            Fraction res = new Fraction();

            // define other variables including res and fr2

            res  = Fraction.add  (fr1, fr2);
            System.out.println(fr1+" + "+fr2+" = "+res);

            res      =      Fraction.subtract (fr1, fr2);
            System.out.println(fr1+" - "+fr2+" = "+res);
            res          =     Fraction.multiply (fr1, fr2);
            System.out.println(fr1+" * "+fr2+" = "+res);
            res          =     Fraction.divide (fr1, fr2);
            System.out.println(fr1+" / "+fr2+" = "+res);
            res          =     Fraction.lessThan (fr1, fr2);
            System.out.println(fr1+" "+res+" "+fr2);

            // test subtract, multiply, divide, lessThan methods
            //    each test has to print a description, a result,
            //    and, possibly, a error message if the calculation fails

    }

}


这是分数:

package Fraction;

public class Fraction
{
protected int a;
protected int b;
public Fraction()
{
    a = 1;
    b = 1;
}
public Fraction (int a, int b)
{
    this.a=a;
    this.b=b;
}
public int getNumerator()
{
    return a;
}
public int getDenominator()
{
    return b;
}
public void setNumerator(int a)
{
    this.a=a;
}

public void setDenominator(int b)
{
    this.b=b;
}
public String toString ()
{
    return a+"/"+b;
}
public int gcd(int a, int b)
{
    //ToDo   implement Euclide algorithm
    if (b==0) return a;
    return gcd(b, a%b);
}
public void lowestTerms()
{
    int g=gcd(a,b);
    a=a/g;
    b=b/g;
}
public static Fraction add(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator()
                      + first.getDenominator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
//ToDo methods subtract, multiply, divide, lessThan
public static Fraction subtract(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator()
                      - first.getDenominator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
public static Fraction multiply(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getNumerator());
    result.setDenominator(first.getDenominator()*second.getDenominator());
    result.lowestTerms();
    return result;
}
public static Fraction divide(Fraction first, Fraction second)
{
    Fraction result = new Fraction();
    result.setNumerator(first.getNumerator()*second.getDenominator());
    result.setDenominator(first.getDenominator()*second.getNumerator());
    result.lowestTerms();
    return result;
}
public static Fraction lessThan(Fraction first, Fraction second)
{
    if (first.getNumerator()*second.getDenominator() <=
            first.getDenominator()*second.getNumerator()){
    return first;
    }
    else {
    return second;
    }
}
}

最佳答案

您必须使用Faction(int, int)构造函数而不是Fraction()构造函数:

Fraction frac = new Fraction ();


一定是

Fraction frac = null;
//later in the code once you have a and b variables set and no exceptions...
frac = new Fraction (a, b);


或使用设置器:

frac.setNumerator(a);
frac.setDenominator(b);


返回frac变量之前。

10-06 07:06