skip to Main Content

I have 1 TextView that provides me two randomly generated numbers 3 + 4 for example, and there are 4 buttons, but a random button has right answer (7) and other 3 – randomly generated numbers. I have no idea how to set a right answer to a random button. What should i use? tags?

2

Answers


  1. I would suggest to use the Random class here.

    public void set_options(int ans){ //ans should contain the original answer
    
      Random random;
      int a;
      a = random.nextInt(4);
      switch(a){
         case 0:
            //Set option 1 to answer
            option1.setText(ans.toString());
    
         case 1:
            //Set option 2 to answer
            option2.setText(ans.toString());
    
         case 2:
            //Set option 3 to answer
            option3.setText(ans.toString());
    
         case 3:
            //Set option 4 to answer
            option4.setText(ans.toString());
    
      }
    }
    
    Login or Signup to reply.
  2. The best way would be to do this.

    Button option1,option2,option3,option4;
    
    private void addOptions(String answer){
        int answerOption = new Random().nextInt(4);
    
        switch (answerOption){
            case 0:
                option1.setText(answer);
                int option1R = new Random().nextInt(99);
                int option2R = new Random().nextInt(99);
                int option3R = new Random().nextInt(99);
                if (option1R != Integer.parseInt(answer)) {
                    option2.setText(new Random().nextInt(99));
                }
                if (option2R != Integer.parseInt(answer)) {
                    option3.setText(new Random().nextInt(99));
                }
                if (option3R != Integer.parseInt(answer)) {
                    option4.setText(new Random().nextInt(99));
                }
                option4.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option3.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option2.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option1.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "correct answer clicked.", Toast.LENGTH_SHORT).show();
                });
                break;
            case 1:
                option1.setText(answer);
                int option1R2 = new Random().nextInt(99);
                int option2R2 = new Random().nextInt(99);
                int option3R2 = new Random().nextInt(99);
                if (option1R2 != Integer.parseInt(answer)) {
                    option1.setText(new Random().nextInt(99));
                }
                if (option2R2 != Integer.parseInt(answer)) {
                    option3.setText(new Random().nextInt(99));
                }
                if (option3R2 != Integer.parseInt(answer)) {
                    option4.setText(new Random().nextInt(99));
                }
                option4.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option3.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option2.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "correct answer click", Toast.LENGTH_SHORT).show();
                });
                option1.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer clicked.", Toast.LENGTH_SHORT).show();
                });
                break;
            case 2:
                option1.setText(answer);
                int option1R22 = new Random().nextInt(99);
                int option2R22 = new Random().nextInt(99);
                int option3R22 = new Random().nextInt(99);
                if (option1R22 != Integer.parseInt(answer)) {
                    option1.setText(new Random().nextInt(99));
                }
                if (option2R22 != Integer.parseInt(answer)) {
                    option2.setText(new Random().nextInt(99));
                }
                if (option3R22 != Integer.parseInt(answer)) {
                    option4.setText(new Random().nextInt(99));
                }
                option4.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option3.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "correct answer click", Toast.LENGTH_SHORT).show();
                });
                option2.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option1.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer clicked.", Toast.LENGTH_SHORT).show();
                });
                break;
            case 3:
                option1.setText(answer);
                int option1R222 = new Random().nextInt(99);
                int option2R222 = new Random().nextInt(99);
                int option3R222 = new Random().nextInt(99);
                if (option1R222 != Integer.parseInt(answer)) {
                    option1.setText(new Random().nextInt(99));
                }
                if (option2R222 != Integer.parseInt(answer)) {
                    option2.setText(new Random().nextInt(99));
                }
                if (option3R222 != Integer.parseInt(answer)) {
                    option4.setText(new Random().nextInt(99));
                }
                option4.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "correct answer click", Toast.LENGTH_SHORT).show();
                });
                option3.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option2.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer click", Toast.LENGTH_SHORT).show();
                });
                option1.setOnClickListener(v -> {
                    Toast.makeText(getApplicationContext(), "wrong answer clicked.", Toast.LENGTH_SHORT).show();
                });
                break;
        }
    }
    

    Note the task of finding the views by id or using binding is your task.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search