vendredi 11 juin 2021

Generate random numbers and add to the list

I am trying to create an application in which I will be offered three answer options (radiobuttons) one will be correct (assigned from the result) but the other two should be randomly generated and different from the correct. It occurred to me to add the correct result to the worksheet and then compare the random number with the correct one and add it to the list as well. Same with the third.

This is my attempt, which is not entirely properly chosen:

public void shuffleText()
    {
        string failResult;
        string wrongResult;
        int failResultN = new int();
        int wrongResultN = new int();
        int wrongNumber = number.Next(1, maxValue);
        failResultN = Int32.Parse(result.Text) - wrongNumber;//I can't do this because my result gets into negative values, which is nonsense
        failResult = failResultN.ToString();            
        wrongResultN = Int32.Parse(result.Text) + wrongNumber;
        wrongResult = wrongResultN.ToString();
        List<string> list = new List<string> { result.Text, failResult, wrongResult };
        var rand = new Random();
        var shuffleText = list.OrderBy(x => rand.Next(list.Count)).ToList();
        var radioButtons = new[] { firstAnswer, secondAnswer, thirdAnswer };
        for (int i = 0; i < radioButtons.Length; i++)
        {
            radioButtons[i].Content = shuffleText[i];
        }
    }

I'm just not sure how to do it. Can anyone advise me?




Aucun commentaire:

Enregistrer un commentaire