简体   繁体   English

Android的随机数学问题生成器

[英]Random math questions generator for Android

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. 问题可以包含2,3或4个整数。 So something like this: 55 2 − 4 − 101, 102/3/3, 589 − 281, 123 + 5 6 + 2. 所以像这样: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. 问题将显示在textview中,然后用户可以猜测,在编辑文本中输入值,然后单击我创建的自定义键盘上的键将检查答案,然后在序列中显示下一个问题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. 非常感谢有时间构建回复的人。

Create an array char[] ops = { '+', '-', '/', '*' } and create a random int i in range [0,3], and chose ops[i] 创建一个数组char[] ops = { '+', '-', '/', '*' }并在范围[0,3]中创建一个随机int i ,并选择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[] 你可以通过创建一个interface MathOp并创建4个实现它的类来使它变得更通用: DivideSum ,...并创建一个数组: MathOp[] ops而不是char[]
Using this, it will also give you much easier time to check the result later on... 使用它,它还可以让您更轻松地在以后查看结果...

A little of spare time produced a complete example for your case. 一些业余时间为您的案例提供了一个完整的例子 Create new RandomMathQuestionGenerator.java file and it is cooked for compilation. 创建新的RandomMathQuestionGenerator.java文件并将其编译以进行编译。

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: 谢谢:

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. 将运算符放在一个数组(4个元素)中,生成一个0到3的随机整数,并选择数组中此索引处的运算符。

Do that each time you need to have a random operator, ie after every number of your question except the last one. 每次你需要一个随机的操作员,即除了最后一个问题之后的每一个问题之后都这样做。

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. 然后生成一个介于0和数组长度减1之间的随机数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM