如何添加减法,乘法和除法以获得用户回答的随机问题,而不仅仅是添加? [英] How do I add subtraction, multiplication and division to get random questions for the user to answer and not just addition?

查看:70
本文介绍了如何添加减法,乘法和除法以获得用户回答的随机问题,而不仅仅是添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加减法,乘法和除法以获得用户回答的随机问题,而不仅仅是添加?就像我如何在我的代码中得到数学方程然后将它们混合起来?

How do I add subtraction, multiplication and division to get random questions for the user to answer and not just addition? Like how do I get the math equations in my code and then mix them up?

public void generateQuestion() {
        Random rand = new Random();
        int a = rand.nextInt(21);
        int b = rand.nextInt(21);
        sumTexrView.setText(Integer.toString(a) + " + " + Integer.toString(b));
        locationOfCorrectAnswer = rand.nextInt(4);
        answers.clear();
        int incorrectAnswer;
        for (int i=0; i<4; i++) {
            if (i == locationOfCorrectAnswer) {
                answers.add(a + b);
            } else {
                incorrectAnswer = rand.nextInt(41);
                while (incorrectAnswer == a + b) {
                    incorrectAnswer = rand.nextInt(41);
                }
                answers.add(incorrectAnswer);
            }
        }
        button0.setText(Integer.toString(answers.get(0)));
        button1.setText(Integer.toString(answers.get(1)));
        button2.setText(Integer.toString(answers.get(2)));
        button3.setText(Integer.toString(answers.get(3)));
    }





我的尝试:



我试过复制for(int i = 0; i< 4; i ++){

if(i == locationOfCorrectAnswer){

answers.add(a + b);

} else {

incorrectAnswer = rand.nextInt(41);

while(incorrectAnswer == a + b){

incorrectAnswer = rand.nextInt(41);

}

answers.add(incorrectAnswer);

}

和put - ,*和/而不是+并将它放在按钮代码上方



What I have tried:

I've tried copying the for (int i=0; i<4; i++) {
if (i == locationOfCorrectAnswer) {
answers.add(a + b);
} else {
incorrectAnswer = rand.nextInt(41);
while (incorrectAnswer == a + b) {
incorrectAnswer = rand.nextInt(41);
}
answers.add(incorrectAnswer);
}
and putting -, * and / instead of + and putting it above the buttons code

推荐答案

你最有可能需要一个包含问题和操作的类来获得答案。它还可以为计算提供方法。使用类的元素填充列表,然后随机选择它们。一旦用户选择了问题,代码将显示文本,接受用户的值,并根据操作计算答案。
You most likely need a class that holds the question and the operation to get the answer. It can also provide the methoids for the calculations. Fill a list with the elements of the class and then select them at random. Once the user has selected a question, the code will display the text, accept the user's values, and calculate the answer based on the operation.


这篇关于如何添加减法,乘法和除法以获得用户回答的随机问题,而不仅仅是添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆