Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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?
C#
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)));
    }


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
Posted
Updated 14-Jun-16 23:02pm
v3
Comments
Maciej Los 14-Jun-16 16:52pm    
Not a question at all! You have to provide more details about your issue and shortly describe what is "not working" and why.
ZCSmouse 14-Jun-16 17:02pm    
I added more :)
Patrice T 14-Jun-16 19:38pm    
Do you understand that we don't know what you talk about.
An explanation of what you want to do would be nice, just to get some context.
ZCSmouse 14-Jun-16 20:09pm    
I'm building a brain training app and the user has to pick from four numbers to get the answer correct. Right now, my app only has addition questions to answer, and I was wondering how to make it to where they can answer subtraction, multiplication and division questions too. I'm sorry I didn't specify enough. :(
ZCSmouse 14-Jun-16 23:27pm    
Do you get what I'm trying to get at?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900