2015-01-11 194 views
0

我正在爲分數乘法和除法的學校編寫一個程序。當我運行當前代碼時,將分數2/4和4/6相乘給我0/1。當我單獨測試2/4和4/6分數的分子/分母的分量時,它給了我0 1 0 1.這裏是輸出和代碼,代碼第一。分數程序不能正常工作

import java.util.*; 
import java.util.Scanner.*; 

class Fraction 
{ 
    protected static int numerator, denominator; 

    public static void main(String args[]) 
    { 
     System.out.println("Chp. 25 LAB 1"); 
     System.out.println(); 


// main commands for testing first class Fraction 

     Fraction f1 = new Fraction(2,4); 
     System.out.println("numerator of fraction1 is " + f1.getNum()); 
     System.out.println("denominator of fraction1 is " + f1.getDen()); 
     f1.displayFraction(); 
     f1.alterFraction(3,6); 
     f1.displayFraction(); 


// main commands for testing second class FractionArithmetic 

     f1 = new Fraction(2,4); 
     Fraction f2 = new Fraction(4,6); 

     FractionArithmetic f3 = new FractionArithmetic(); 
     f3.multFracts(f1, f2); 
     f3.display(); 

     f3 = new FractionArithmetic(); 
     f3.divFracts(f1, f2); 
     f3. display(); 


// main commands for testing second class FractionOperations 

     f1 = new Fraction(2,4); 
     f2 = new Fraction(4,6); 

     FractionOperations fract = new FractionOperations(); 
     fract.multFracts(f1, f2); 
     fract.display(); 
     fract.reduce(); 
     fract.getFraction().displayFraction(); 

     fract = new FractionOperations(); 
     fract.divFracts(f1, f2); 
     fract.display(); 
     fract.reduce(); 
     fract.getFraction().displayFraction(); 
    } 

    Fraction(int n, int d) 
    { 
     numerator = n; 
     denominator = d; 
    } 
    public int getNum() 
    { 
     return numerator; 
    } 
    public int getDen() 
    { 
     return denominator; 
    } 
    public void alterFraction(int n, int d) 
    { 
     numerator = n; 
     denominator = d; 
    } 
    public void displayFraction() 
    { 
     System.out.println("Fractioni = " + numerator + "/" + denominator); 
    } 
} 

class FractionArithmetic 
{ 
    protected Fraction fraction; 

    FractionArithmetic() 
    { 
     fraction = new Fraction(0, 1); 
    } 
    FractionArithmetic(int n, int d) 
    { 
     fraction = new Fraction(n, d); 
    } 
    public void multFracts(Fraction a, Fraction b) 
    { 
//  System.out.println(a.getNum() + " " + a.getDen() + " " + b.getNum() + " " + b.getDen()); 
     fraction.alterFraction(a.getNum() * b.getNum(), a.getDen() * b.getDen()); 
    } 

    public void divFracts(Fraction a, Fraction b) 
    { 
     fraction.alterFraction(a.getNum() * b.getDen(), a.getDen() * b.getNum()); 
    } 
    public void display() 
    { 
     System.out.println("This is an unreduced fraction"); 
     fraction.displayFraction(); 
    } 
    public Fraction getFraction() 
    { 
     return fraction; 
    } 
} 

class FractionOperations extends FractionArithmetic 
{ 
    Fraction reducedFraction; 

    FractionOperations() 
    { 
     reducedFraction = new Fraction(0, 1); 
    } 
    FractionOperations(int n, int d) 
    { 
     super(n, d); 
     reducedFraction = new Fraction(n, d); 
    } 
    public void reduce() 
    { 
     int gcf = getGCF(reducedFraction.numerator, reducedFraction.denominator); 
     fraction.alterFraction(reducedFraction.numerator/gcf, reducedFraction.denominator/gcf); 
    } 
    private static int getGCF(int a, int b) 
    { 
     int gcf = 1; 
     a = Math.abs(a); 
     b = Math.abs(b); 
     if(a > b) 
     { 
      int dummy = b; 
      b = a; 
      a = dummy; 
     } 
     for(int i = 1; i <= b; i++) 
     { 
      if(b % i == 0 && a % i == 0) 
       gcf = i; 
     } 
     return gcf; 
    } 
    public void display() 
    { 
     reducedFraction.displayFraction(); 
     fraction.displayFraction(); 
    } 
    public Fraction getFraction() 
    { 
     return reducedFraction; 
    } 

} 

分子fraction1的是2
分母fraction1的是4
Fractioni = 2/4
Fractioni = 3/6
Fractioni = 0/1
這是未減數分數
分數= 0/0
分數= 0/1
分數= 0/1
Fractioni = 0/1
Fractioni = 0/0
Fractioni = 0/0
Fractioni = 0/0

+1

您是否嘗試過使用調試器?說真的,你正在學習編程,你需要學習的技能之一就是如何調試你自己的代碼。如果你不嘗試去做,你就不會學習。 –

回答

0

也許,那是因爲你的類使它它除以2/4 3/6。我認爲編程使用像2/4字面的數字,而不是單獨產生數字,例如1和0。