为什么getFraction1和getFraction2方法没有正确设置这些变量的值?

时间:2013-06-22 18:38:37

标签: java try-catch

我试图以这样一种方式编写TestFraction,即main方法调用方法getFraction1getFraction2来创建两个Fraction s:fr1(a / b)和fr2(a / b)。 getFraction1getFraction2会提示用户输入两个整数ab,然后调用getNumber来捕获这些整数。然后,分数在fr1和fr2上执行计算。问题是,当我运行TestFraction时,a和b的值保留为1,这是它们在Fraction中设置的值。任何人都可以给我一个提示,为什么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;
    } 
}
}

2 个答案:

答案 0 :(得分:1)

您必须使用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);

OR 使用setter:

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

返回frac变量之前。

答案 1 :(得分:0)

看起来您没有设置从控制台中读取的值

a = getNumber();
b = getNumber();

frac.setNumerator(a);
frac.setDenominator(b);
默认情况下,

ab设置为1,当您说

Fraction frac = new Fraction();

因为Fraction默认构造函数定义如此

public Fraction()
{
    a = 1;
    b = 1;
}