mardi 23 février 2016

How do I verify this equation generator with three operands and two operators?

How do I verify this equation generator with three operands and two operators? Each time I run it it is always wrong even when I input the correct answer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Random {

    public static void main(String[] args) {
        System.out.println("Difficulty Random Generation Algorithms:");
        easyDifficulty();
    }



    public static void easyDifficulty() {
        int operand1, operand2, operand3, operator1, operator2;
        operand1 = (int) (Math.random() * 25) + 1; // questions will only contain operands up to the value of 25.
        operand2 = (int) (Math.random() * 25) + 1; //"
        operand3 = (int) (Math.random() * 25) + 1; //"
        operator1 = (int) (Math.random() * 4) + 1; //Possibility of four operators: +, -, * and /.
        operator2 = (int) (Math.random() * 4) + 1; //"

        String operation1 = null; //String initially null value.
        int actualAnswer = 0; //int initially zero value.

        switch (operator1) {
            case 1:
                operation1 = "+";
                actualAnswer = operand1 + operand2;
                break;
            case 2:
                operation1 = "-";
                actualAnswer = operand1 - operand2;
                break;
            case 3:
                operation1 = "*";
                actualAnswer = operand1 * operand2;
                break;
            case 4:
                operation1 = "/";
                actualAnswer = operand1 / operand2;
                break;
        }
        String operation2 = null; //String initially null value.
        switch (operator2) {
            case 1:
                operation2 = "+";
                actualAnswer = operand1 + operand2;
                break;
            case 2:
                operation2 = "-";
                actualAnswer = operand1 - operand2;
                break;
            case 3:
                operation2 = "*";
                actualAnswer = operand1 * operand2;
                break;
            case 4:
                operation2 = "/";
                actualAnswer = operand1 / operand2;
                break;
        }

        System.out.println("Question: " + operand1 + operation1 + operand2 + operation2 + operand3);
        String yourGuess = null;
        try {
            System.out.print("Your guess: ");
            yourGuess = new BufferedReader(new InputStreamReader(System.in)).readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (actualAnswer == Integer.parseInt(yourGuess)) {
            System.out.println("Correct!");
        } else {
            System.out.println("\nIncorrect!");
            System.out.println("Correct answer: " + actualAnswer);
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire