Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a small problem, I'm trying to create a math quiz in WPF Apllication, where numbers will randomly pop up, which will be calculated according to the chosen math operation.
Everything works as planned, but now I've gotten to prepare Radiobutton and I have no idea how to come up with it. I would like to do this so that the Radiobuttons would take turns which answer would be correct (so that it was still not the first correctly). I know the answer will have to be written as Radiobutton's content. But I don't know how to set the condition for checking the correctness of the result.

I will be happy for any advice or nudges in the right direction.

What I have tried:

C#
if (firstPoss.Content == result.Text)

               {
               }
               if (secondPoss.Content == result.Text)

               {
               }
               if (thirdPoss.Content == vresult.Text)

               {
               }


this IF statement always passes
Posted
Updated 10-May-21 21:57pm

1 solution

Put your radio buttons in an array (or any other Collection) so you can access them by a numeric index.
Then use the Random class[^] to decide which one is "correct" and use that value as the index. All the others are the false answers.
 
Share this answer
 
Comments
Member 15170612 11-May-21 4:55am    
idea: one could assign that the correct answer will always be the first radiobutton but I will randomly shuffle it with some function?
OriginalGriff 11-May-21 5:21am    
Shuffling is pretty easy:
Copy the array of buttons (filled in, ready to rock'n'roll) to a List, called "Pack"
Create a new, empty List called "Shuffled".
Loop while there are any items in the Pack:
1) Use Random to generate an index into current Pack (0 to current elements count - 1).
2) Use the index to add that button to the end of the Shuffled.
3) Remove the item at that index from the List.
After the loop, Pack is empty, Shuffled contains the buttons in a random order. So you can now use that to display your buttons!
Member 15170612 11-May-21 7:03am    
Thank you very much !!!
OriginalGriff 11-May-21 7:17am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900