2016-09-26 11 views
-1

私はJavaでランダム演算を使って小数計算を作成しようとしていますが、10の質問があります。私の問題はanswerKeyです。 StringをFractionに変換できないので、answerKeyのメソッドに間違ったメソッドがあると思います。私はまだそれを修正する方法を知らない。ここランダム演算子をJavaでランダムに計算する

は私のコードです:

public class TesterFractionTugas1 { 
    public static void main(String[] args){ 
    Scanner in    = new Scanner(System.in); 
    Random rand    = new Random(); 
    final int jumlahSoal  = 10; 
    int count    = 0; 
    String output   = ""; 

    while(count < jumlahSoal){ 
     int angka1 = (int) (Math.random() * 10 + 1); 
     int angka2 = (int) (Math.random() * 10 + 1); 
     int angka3 = (int) (Math.random() * 10 + 1); 
     int angka4 = (int) (Math.random() * 10 + 1); 

     char operator = genOperator(rand.nextInt(3)); 

     Fraction pecahan1 = new Fraction(angka1, angka2); 
     Fraction pecahan2 = new Fraction(angka3, angka4); 

     System.out.println("Question :" + evaluateQuestion(pecahan1.getFraction(), pecahan2.getFraction(), operator)); 
     System.out.println("AnswerKey: " + answerKey(pecahan1, pecahan2, operator)); // wrong code in here 

     System.out.println("your answer: "); 
     Fraction pecahanAnswer = new Fraction(); 
     String answer   = in.nextLine(); 
     String[] sptext  = new String[2]; 

     sptext = answer.split("/"); 
     pecahanAnswer.setNumerator(Integer.parseInt(sptext[0])); 
     pecahanAnswer.setDenominator(Integer.parseInt(sptext[1])); 

     if(answer == answerKey(pecahan1, pecahan2, operator)){ 
      System.out.println("You right"); 
      count++; 
     } else { 
      System.out.println("You Wrong"); 
      count++; 
     } 
    }  
} 

// giving operator for math 
public static char genOperator(int a){ 
    switch(a){ 
     case 0: return '+'; 
     case 1: return '-'; 
     case 2: return '*'; 
     case 3: return '/'; 
     default: return '+'; 
    } 
} 

// the question here 
public static String evaluateQuestion(String pecahan1, String pecahan2, char operator){ 
    return pecahan1 + " " + operator + " " + pecahan2; 
} 

// answerkey 
public static String answerKey(Fraction pecahan1, Fraction pecahan2, char operator){ 
    switch (operator){ 
     case '+': pecahan1.addFraction(pecahan2); 
     case '-': pecahan1.subtractFraction(pecahan2); 
     case '*': pecahan1.multiplyByFraction(pecahan2); 
     case '/': pecahan1.divideByFraction(pecahan2); 
     default: return null; 
    } 

    } 
} 


class Fraction { 
    private int numerator, denominator; 

public Fraction(){ 
    numerator = 0; 
    denominator = 1; 
} 

public Fraction(int numerator, int denominator){ 
    this.numerator = numerator; 
    this.denominator = denominator; 
} 

public void setNumerator(int numerator){ 
    this.numerator = numerator; 
} 

public int getNumerator(){ 
    return this.numerator; 
} 

public void setDenominator(int denominator){ 
    this.denominator = denominator; 
} 

public int getDenominator(){ 
    return this.denominator; 
} 

public String getFraction() { 
    return numerator + "/" + denominator; 
} 

public String toString(){ 
    return numerator + "/" + denominator; 
} 

public String addFraction(Fraction pecahan2) { 
    // Sum formula: a/b + c/d = (ad + cb)/bd 
    int a = this.numerator; 
    int b = this.denominator; 
    int c = pecahan2.numerator; 
    int d = pecahan2.denominator; 
    return ((a*d) + (c*b)) + "/" + (b*d); 
} 

public String subtractFraction(Fraction pecahan2) { 
    // Subtraction formula: a/b - c/d = (ad - cb)/bd 
    int a = this.numerator; 
    int b = this.denominator; 
    int c = pecahan2.numerator; 
    int d = pecahan2.denominator; 
    return ((a*d) - (c*b)) + "/" + (b*d); 
} 


public String multiplyByFraction(Fraction pecahan2) { 
    // Multiplication formula: a/b * c/d = ac/bd 
    int a = this.numerator; 
    int b = this.denominator; 
    int c = pecahan2.numerator; 
    int d = pecahan2.denominator; 
    return (a*c) + "/" + (b*d); 
} 

public String divideByFraction(Fraction pecahan2) { 
    // Division formula: (a/b)/(c/d) = ad/bc 
    int a = this.numerator; 
    int b = this.denominator; 
    int c = pecahan2.numerator; 
    int d = pecahan2.denominator; 
    return (a*d) + "/" + (b*c); 
    } 
} 

私はあなたがあなたのanswerKey方法でreturn文はこれにそれを変更不足している私に

答えて

0

をnull値を与えていないことをanswerKey修正する方法を意味します。

スイッチケースが破損していない場合は、すべてを実行してください。

public static String answerKey(Fraction pecahan1, Fraction pecahan2, char operator) { 
     switch (operator) { 
     case '+': 
      return pecahan1.addFraction(pecahan2); 
     case '-': 
      return pecahan1.subtractFraction(pecahan2); 
     case '*': 
      return pecahan1.multiplyByFraction(pecahan2); 
     case '/': 
      return pecahan1.divideByFraction(pecahan2); 
     default: 
      return null; 
     }