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
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