Random math questions generator for Android

2020-06-27 08:54发布

问题:

Any help or advice would be greatly appreciated. I'm trying to create a simple game which generates ten different, random questions. The questions can contain 2, 3 or 4 integers. So something like this: 55 2 − 4 − 101, 102/3/3, 589 − 281, 123 + 5 6 + 2.

The question will be displayed in a textview and then the user can take a guess, entering values into an edittext and then upon clicking a key on a custom keypad I have created it will check the answer, and then display the next question in the sequence of 10.

I know how to create random numbers, just struggling to work out how to create a whole question with random operators (+, -, /, *).

Big thank you to anyone who has the time to construct a reply.

回答1:

A little of spare time produced a complete example for your case. Create new RandomMathQuestionGenerator.java file and it is cooked for compilation.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class RandomMathQuestionGenerator {

    private static final int NUMBER_OF_QUESTIONS = 10;
    private static final int MIN_QUESTION_ELEMENTS = 2;
    private static final int MAX_QUESTION_ELEMENTS = 4;
    private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
    private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
    private final Random randomGenerator = new Random();

    public static void main(String[] args) {
        RandomMathQuestionGenerator questionGenerator = new RandomMathQuestionGenerator();
        List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions();
        for (Question question : randomQuestions) {
            System.out.println(question);
        }
    }

    public List<Question> getGeneratedRandomQuestions() {
        List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS);
        for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) {
            int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity();
            Question question = new Question(randomQuestionElementsCapacity);
            for (int j = 0; j < randomQuestionElementsCapacity; j++) {
                boolean isLastIteration = j + 1 == randomQuestionElementsCapacity;

                QuestionElement questionElement = new QuestionElement();
                questionElement.setValue(getRandomQuestionElementValue());
                questionElement.setOperator(isLastIteration ? null
                        : Operator.values()[randomGenerator.nextInt(Operator.values().length)]);

                question.addElement(questionElement);
            }
            randomQuestions.add(question);
        }
        return randomQuestions;
    }

    private int getRandomQuestionElementsCapacity() {
        return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS);
    }

    private int getRandomQuestionElementValue() {
        return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE);
    }

    private int getRandomIntegerFromRange(int min, int max) {
        return randomGenerator.nextInt(max - min + 1) + min;
    }
}

class Question {

    private List<QuestionElement> questionElements;

    public Question(int sizeOfQuestionElemets) {
        questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets);
    }

    public void addElement(QuestionElement questionElement) {
        questionElements.add(questionElement);
    }

    public List<QuestionElement> getElements() {
        return questionElements;
    }

    public int size() {
        return questionElements.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (QuestionElement questionElement : questionElements) {
            sb.append(questionElement);
        }
        return sb.toString().trim();
    }
}

class QuestionElement {

    private int value;
    private Operator operator;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    @Override
    public String toString() {
        return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " ";
    }
}

enum Operator {

    PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/");
    private String displayValue;

    private Operator(String displayValue) {
        this.displayValue = displayValue;
    }

    public String getDisplayValue() {
        return displayValue;
    }
}

Run and preview. Hope this helps.

Thanks to:

  • Generating random number in range
  • Retrieving random element from array


回答2:

Create an array char[] ops = { '+', '-', '/', '*' } and create a random int i in range [0,3], and chose ops[i]

You will need to take care that you do not generate a divide by zero question.

You can make it even more generic by creating an interface MathOp and creating 4 classes that implement it: Divide, Sum , ... and create an array: MathOp[] ops instead of the char[]
Using this, it will also give you much easier time to check the result later on...



回答3:

Put your operators in an array (4 elements), generate a random integer from 0 to 3, and pick the operator that is at this index in the array.

Do that each time you need to have a random operator, i.e. after every number of your question except the last one.



回答4:

Make an array that has one entry for each of the operators. Then generate a random number between 0 and the length of the array minus 1.



回答5:

So since each operation is binary you can just worry about figuring out the base case and then building up your expressions from there. An easy way would just to select a random number an correlate that which operation will be used.

int displayAnswer(int leftSide, int rightSide, int operation {
int answer;
string operation;
    switch(operation) {
        case 1:
            operation = "+";
            answer = leftSide + rightSide;
            break;
        case 2:
            operation = "-";
            answer = leftSide - rightSide;
            break;
        case 3:
            operation = "*";
            answer = leftSide * rightSide;
            break;
        case 4:
            operation = "/";
            answer = leftSide / rightSide:
            break;
    }
    textView.setText(leftSide + operation + rightSide);
    return answer;
}