简体   繁体   中英

c# Lottery looping program with Visual Studio

I am currently trying to figure out how to create a program within Visual Studio, that matches 3 numbers, 1-9, given in a textbox with 3 random generated numbers in a listbox. I am having trouble figuring out how to write a WHILE looping statement that'll allow for my listbox to randomly generate 3 numbers at a time and continue if there is no match, but breaks if there is a match, with a max of 1000 attempts. I am a bit lost on how to do this. Thank you for your help.

        private void LuckyButton_Click(object sender, EventArgs e)
    {
        Random RandomNumber = new Random();
        string newLine = Environment.NewLine;
        int Winning1 = 0;
        int Winning2 = 0;
        int Winning3 = 0;
        int numbers = RandomNumber.Next(10);

        int.TryParse(WinningNumber1.Text, out Winning1);
        int.TryParse(WinningNumber2.Text, out Winning2);
        int.TryParse(WinningNumber3.Text, out Winning3);


        if (Winning1 <= 0 || Winning2 <= 0 || Winning3 <= 0)
        {
            MessageBox.Show("Enter a number!");
            return;
        }

        while (numbers <= 1000)
        {
            numbers = RandomNumber.Next(10);
        }

    }

See if this help you:

 private void LuckyButton_Click(object sender, EventArgs e)
    {
        Random RandomNumberGenerator = new Random();
        string newLine = Environment.NewLine;
        int Winning1 = 0;
        int Winning2 = 0;
        int Winning3 = 0;
        int randomNumber = RandomNumberGenerator.Next(10);
        int counter = 0;
        int winnerId = 0;


        int.TryParse(WinningNumber1.Text, out Winning1);
        int.TryParse(WinningNumber2.Text, out Winning2);
        int.TryParse(WinningNumber3.Text, out Winning3);


        if (Winning1 <= 0 || Winning2 <= 0 || Winning3 <= 0 ||
            Winning1 > 10 || Winning2 > 10 || Winning3 > 10)
        {
            MessageBox.Show("Invalid Number!");
            return;
        }

        while (counter < 1000)
        {
            if (Winning1 == randomNumber)
            {
                winnerId = 1;
                break;
            }
            else if (Winning2 == randomNumber)
            {
                winnerId = 2;
                break;
            }
            else if (Winning3 == randomNumber)
            {
                winnerId = 3;
                break;
            }
            randomNumber = RandomNumberGenerator.Next(10);
            counter++;
        }

        if(winnerId != 0)
        {
            MessageBox.Show("Number " + winnerId + " wins!");
        }
        else
        {
            MessageBox.Show("no one wins!");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM