随机分数与Java中的随机算子计算器

时间:2016-09-26 16:34:53

标签: java random

我试图用Java创建分数计算器并随机操作,会有10个问题,我的问题是answerKey总是给我回答null。我认为错误的方法是在answerKey的方法中,因为不能将String转换为Fraction。我仍然不知道如何解决它。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

你缺少answerKey方法中的return语句,将其改为此。

你应该总是寻找打破开关的情况,否则所有这些都将被执行。

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;
        }