1

I'm trying to generate random operators in order to make the random questions have random operators, please help

public void generateNewAddQuestion () {
        Random rand = new Random();
        int a = rand.nextInt(21);
        int b = rand.nextInt(21);
        int c =0;

        sumTextView.setText(Integer.toString(a) + "+" + Integer.toString(b));
        locationOfCorrecAnswer = rand.nextInt(4);
        answers.clear();
        String[] operators = {"+", "-", "x", "/"};

        int incorrectAnswer;

        for (int i=0; i < 4; i++) {
            if (i == locationOfCorrecAnswer) {
                answers.add(a+b);
                sumTextView.setText(Integer.toString(a)+"+"+Integer.toString(b));
            }
            else {
                incorrectAnswer = rand.nextInt(41);
                while(incorrectAnswer == a+b) {
                    incorrectAnswer = rand.nextInt(41);
                }
                answers.add(incorrectAnswer);
            }
        }
        button2.setText(Integer.toString(answers.get(0)));
        button3.setText(Integer.toString(answers.get(1)));
        button4.setText(Integer.toString(answers.get(2)));
        button5.setText(Integer.toString(answers.get(3)));
    }
hata
  • 11,633
  • 6
  • 46
  • 69
cams
  • 25
  • 5
  • I also have four buttons for the user interface, I just need a way of making the operators more dynamic, currently, the addition is working perfectly – cams Apr 18 '20 at 08:26
  • also before someone completely brushes me off and say that this question had been asked before, I have done my research and from my point of view, the ones that I have just checked ain't similar to this, thanks in advance – cams Apr 18 '20 at 08:28
  • Rather than being defensive, it's more polite to list the questions that are similar but not duplicates. – NomadMaker Apr 18 '20 at 13:21

1 Answers1

1

You need to use operators[rand.nextInt(4)] in order to get a random operator e.g.

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        String[] operators = { "+", "-", "x", "/" };
        Random rand = new Random();
        System.out.println(operators[rand.nextInt(4)]);
    }
}

If you execute this code, it will print a random operator every time.

[Update]

This is how you need to use the solution in your program:

String[] operators = {"+", "-", "x", "/"};
int oprIndex = rand.nextInt(4);
sumTextView.setText(Integer.toString(a) + operators[oprIndex] + Integer.toString(b));

Later in your program, you will need to use oprIndex to find which operator has been set into sumTextView and accordingly you can perform the corresponding arithmetic operation e.g.

switch (operators[oprIndex]) {
case "+":
    System.out.println("Pocessing addition");
    break;
case "-":
    System.out.println("Pocessing subtraction");
    break;
case "x":
    System.out.println("Pocessing multiplication");
    break;
case "/":
    System.out.println("Pocessing division");
    break;
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • hello, do you have a suggestion on how I can use your suggested code in order to generate new questions each time with different operators? the only downside of my code is that it is only possible to generate random numbers each time but with only the addition operator. i want the new question generated to have either subtraction or multiplication – cams Apr 18 '20 at 13:15
  • @cams - I've posted an update. Let me know if you've any further doubt. – Arvind Kumar Avinash Apr 18 '20 at 13:31
  • if (oprIndex == '+'){ if(i == locationOfCorrecAnswer){} }else if(oprIndex == '-'){ locationOfCorrecAnswer = a-b; }else if(oprIndex == '*'){ locationOfCorrecAnswer = a*b; } – cams Apr 18 '20 at 15:09
  • i tried comparing it this way and got an error, this is my last question thanks inadvance – cams Apr 18 '20 at 15:09
  • you said you were updating it to let me know how to use the oprindex?? meanwhile, I'm trying to see how I can use switch statements instead – cams Apr 18 '20 at 15:26
  • I've posted the update. Feel free to let me know if you need any further help. – Arvind Kumar Avinash Apr 18 '20 at 15:40