Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to make random check Java?

Posted on November 11, 2022 By

Solution 1 :

You better keep your buttons in the array.
Also the disabled buttons need to be tracked in order to disable exactly 2 distinct buttons, otherwise the counter is incremented even if the button has already been disabled:

            Random rand = new Random(); //initialize generator here only once
            Set<Integer> disabledButtons = new HashSet<Integer>();
            int a=0;
            Button[] buttons = new Button[4]; //keep buttons here
            while(a<2)
            {
                int random = rand.nextInt(4);

                if(buttons[random].getText()!=mAnswerFi && !disabledButtons.contains(random)) {
                        buttons[random].setText("");
                        buttons[random].setEnabled(false);
                        disabledButtons.add(random);
                        a++;
                }

            }//end while

Problem :

I have a quiz and when user clicks on “50/50 ” button there should left only correct answer and one random incorrect answer.

int a=0;
            while(a!=2){
                int random = new Random().nextInt(4)+1 ;

                if(random%4==0) {
                    if(answer4.getText()!=mAnswearFi){
                        answer4.setText("");
                        answer4.setEnabled(false);
                        a++;
                    }

                }
                if(random%3==0) {
                    if(answer3.getText()!=mAnswearFi){
                        answer3.setText("");
                        answer3.setEnabled(false);
                        a++;
                    }
                }
                if(random%2==0) {
                    if(answer2.getText()!=mAnswearFi){
                        answer2.setText("");
                        answer2.setEnabled(false);
                        a++;
                    }

                }
                 if (random%1==0) {
                    if(answer1.getText()!=mAnswearFi){
                        answer1.setText("");
                        answer1.setEnabled(false);
                        a++;
                    }

                }

            }

( mAnswerFi here – it is a correct answer ; answer1,2,3,4 – buttons that will be pressed.)
Sometimes it lefts 3 answers or crashes without any changing ( text on buttons doesnt disappear on all 4 buttons) but i need 2 answers to be left. What should i do?
Thanks in advance!

Comments

Comment posted by Jugjin

It’s more simple solutions. Thanks. But on the same questionwhen i press 50/50 button – there can be 2 questions left. and when i start game one more time on this question after pressing 50/50 button – 3 questions left. Do you have any idea why ? Maybe because first random number is 4 and second will be also 4(and a ==2 in this case). And that is why there are 3 questions left.

Comment posted by mangusta

@Jugjin you need to instantiate random number generator only once, and use it whenever you generate a random number (you were instantiating it everytime when you generated the number, in that case you may get the same number all the time, which results in infinite loop and you see 3 questions left). I have updated my answer because I missed that point too, initially

READ  [FIXED] node.js - Appcelerator CLI fails to launch Android App after updating from 8.2.0.GA to 9.0.X.GA SDK
Powered by Inline Related Posts

Comment posted by Jugjin

Yes, i updated it but still sometimes can be 3 answers left . Maybe we should create a list of random numbers ? Because in our variant there is still chance that second number will be the same as previous ?

Comment posted by mangusta

@Jugjin given that the generator is initialized only once, the probability of two same random numbers is 1/4, three random numbers (1/4)*(1/4), four random numbers (1/4)*(1/4)*(1/4), etc. In other words it is very low, so the loop will eventually hit a=2 and exit. You still see 3 answers left?

Comment posted by mangusta

@Jugjin, oh I’m sorry, there is a bug in my code – you need to keep track of already disabled buttons, otherwise you will increment “a” even if the button has already been disabled. I will update again

Android Tags:android, java

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] android – Draw arrow head in canvas using Angle Android
[FIXED] android – How to input fractional numbers in editText? Android
[FIXED] android – First argument to verifyIdToken() must be a Firebase ID token string error Android
[FIXED] Android App Bundle kills Google Play Games integration – Working with plain old APK – How can I fix this? Android
[FIXED] alarm – How to trigger the Sonic Bomb device to shake from a third-party Android app? Android
[FIXED] mvvm – How to Implement ViewModel Factory in Android Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme