title: Simple Math game - Android
body: <p>I'm new to android. I tried creating a math puzzle. Once the user hits start, two numbers are randomly generated and there are four buttons where the answers are shown. However only one is correct and the others are incorrect. It works fine if there is only one operation allowed like addition or subtraction. However when I allow addition, subtraction and multiplication, I get duplicate answers in the buttons. Can someone please tell me where I'm going wrong. The code below works fine as only addition is allowed. </p>
  
  <pre><code>    public void generateQuestion(){
  
      Random rand = new Random();
  
      int a = rand.nextInt(21);
      int b = rand.nextInt(21);
  
      sumTextView.setText(Integer.toString(a) + "+" +Integer.toString(b)); //sumTextView shows the operation on the screen
      locationOfCorrectAnswer = rand.nextInt(4);
      answers.clear();
      int incorrectAnswer;
      for(int i=0; i&lt;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)));
  
  }
  </code></pre>
  
  <p>The code below doesn't work.</p>
  
  <pre><code>public void generateQuestion(){
  
      Random rand = new Random();
  
      int a = rand.nextInt(21);
      int b = rand.nextInt(21);
  
      ArrayList&lt;String&gt; mathOperations = new ArrayList&lt;String&gt;();
      mathOperations.add("+");
      mathOperations.add("-");
      mathOperations.add("X");
  
      String randomOperator = mathOperations.get(rand.nextInt(3));
  
      locationOfCorrectAnswer = rand.nextInt(4);
      answers.clear();
      int incorrectAnswer;
     for(int i=0; i&lt;4;i++){
          if(i==locationOfCorrectAnswer){
              if(randomOperator.equals("+")){
                  a = rand.nextInt(21);
                  b = rand.nextInt(21);
                  sumTextView.setText(Integer.toString(a) + randomOperator +Integer.toString(b));
                  answers.add(a+b);
              }else if(randomOperator.equals("-")){
                  a = rand.nextInt(21);
                  b = rand.nextInt(21);
                  if(a&gt;b){
                      answers.add(a-b);
                  } else{
                      sumTextView.setText(Integer.toString(b) + randomOperator +Integer.toString(a));
                      answers.add(b-a);
                  }
  
              }else if(randomOperator.equals("X")){
  
                  a=rand.nextInt(14);
                  b=rand.nextInt(14);
                  sumTextView.setText(Integer.toString(a) + randomOperator +Integer.toString(b));
                  answers.add(a*b);
              }
          }
  
          else {
  
                  if(randomOperator.equals("+") || randomOperator.equals("-")) {
                      incorrectAnswer = rand.nextInt(61);
                      while(incorrectAnswer == a+b || incorrectAnswer == a-b ){
                          incorrectAnswer = rand.nextInt(61);
                  }
                      answers.add(incorrectAnswer);
  
                  } else{
                      incorrectAnswer = rand.nextInt(200);
                      while(incorrectAnswer == a*b){
                          incorrectAnswer = rand.nextInt(200);
                      }
                      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)));
  
  }
  </code></pre>