简体   繁体   中英

C# simple Lottery Program

This works fine as long as all 3 numbers are different, but if the user inputs 2 or more of the same number and that number matches at least 1 of the random numbers, it comes out with the 3 match outcome ($1000).

What can I do to make sure if the user enters 2 or more of the same number it won't come out as the 3 match?

namespace test2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int e1, e2, e3;
            int matches = 0;

            e1 = Convert.ToInt32(textBox1.Text);
            e2 = Convert.ToInt32(textBox2.Text);
            e3 = Convert.ToInt32(textBox3.Text);


            Random LotteryNum = new Random();

            int num1 = LotteryNum.Next(1, 4);
            int num2 = LotteryNum.Next(1, 4);
            int num3 = LotteryNum.Next(1, 4);


            label2.Text = " The winning nummbers are " + num1 + num2 + num3;

            if (e1 == num1 && e2 == num2 && e3 == num3)
            {
                ++matches;
            }
            if (e1 == num1 || e1 == num2 || e1 == num3 && e1 != e2 && e1 != e3)
            {
                ++matches;
            }

            if (e2 == num1 || e2 == num2 || e2 == num3 && e2 != e1 && e2 != e3)
            {
                ++matches;
            }

            if (e3 == num1 || e3 == num2 || e3 == num3 && e3 != e1 && e3 != e2)
            {
                ++matches;
            }

            if (matches == 1)
            {
                label1.Text = "Congratulations! You have won $10!\n";
            }
            else
            {
                if (matches == 2)
                {
                    label1.Text = "Congratulations! You have won $100!\n";
                }
                else
                {
                    if (matches == 3)
                    {
                        label1.Text = "Congratulations! You have won $1,000!\n";
                    }
                    else
                    {
                        if (matches == 4)
                        {
                            label1.Text = "Congratulations! You have won $10,000!!!\n";
                        }
                        else
                        {
                            label1.Text = "I'm sorry, you didn't win.";
                        }
                    }

                }
            }


        }
    }
}

Here is a very simplified way to do this:

public void Button1_Click(object sender, EventArgs e)
{
    List<int> userNums = new List<int>();
    List<int> lotteryNums = new List<int>();

    userNums.Add(Convert.ToInt32(textbox1.Text));
    userNums.Add(Convert.ToInt32(textbox2.Text));
    userNums.Add(Convert.ToInt32(textbox3.Text));

    Random LotteryNum = new Random();

    lotteryNums.Add(LotteryNum.Next(1, 4));
    lotteryNums.Add(LotteryNum.Next(1, 4));
    lotteryNums.Add(LotteryNum.Next(1, 4));

    lotteryNums.Remove(userNums[0]);
    lotteryNums.Remove(userNums[1]);
    lotteryNums.Remove(userNums[2]);

    if (lotteryNums.Count == 3)
        label1.Text = "You didn't get any matches";
    else if (lotteryNums.Count == 2)
        label1.Text = "You made one match!";
    else if (lotteryNums.Count == 1)
        label1.Text = "You made two matches!";
    else if (lotteryNums.Count == 0)
        label1.Text = "You made three matches, jackpot!";
}

It uses two lists, one for the numbers that the user enters, and one for the random numbers. It doesn't matter if they are duplicate, it can only match once. The lotteryNums.Remove will remove the first instance of the matching number, so even if the user enters the numbers twice, it will only credit them for one match.

Also notice how the if statement is formatted, and how much easier it is to read and follow. Try to avoid arrow code, if your if statements are more than 2 or 3 levels deep, you may want to rethink how you are doing them.

Edit

If you want to count matches, its very simple, change the following:

    int matches = 0;
    matches += lotteryNums.Remove(userNums[0]) ? 1 : 0;
    matches += lotteryNums.Remove(userNums[1]) ? 1 : 0;
    matches += lotteryNums.Remove(userNums[2]) ? 1 : 0;

The ?: is called the ternary operator, and its like a short-hand if that evaluates to:

    if (lotteryNums.Remove(userNums[0]) == true)
        matches += 1;
    else
        matches += 0;

Then you can use it in your if statement:

if (matches == 0)
    label1.Text = "You didn't get any matches";
else if (matches == 1)
    label1.Text = "You made one match!";
else if (matches == 2)
    label1.Text = "You made two matches!";
else if (matches == 3)
    label1.Text = "You made three matches, jackpot!";

In the future, when you can use loops, you can simplify it further:

foreach(var userNum in userNums)
    matches += lotteryNums.Remove(userNum) ? 1 : 0;

Which gives you the flexibility to make a lottery system with different amounts of numbers without having to change any of your code (other than the if part that determines winnings, but you can get around that too with another List or Dictionary).

I would recommend filling two Lists of integers, one with your lottery numbers and one with the user input. Then you could loop over them and count the matches. This would also allow flexibility if you wanted to allow for position-independent matches by using IndexOf() .

You want to store the input integers and random generated integers in two separate lists respectively. Then you want to remove any duplicates before you count the number of matches. So for your example 123 and 313 would become 123 and 31 which would result in two matches. Here is code on how to do it with just if statements:

        private void button1_Click(object sender, EventArgs e)
        {
            var e1 = int.Parse(textBox1.Text);
            var e2 = int.Parse(textBox2.Text);
            var e3 = int.Parse(textBox3.Text);

            //Code to display winning numbers here

            var input = new List<int>();
            input.Add(e1);
            if (!input.Contains(e2))
                input.Add(e2);
            if (!input.Contains(e3))
                input.Add(e3);

            var lotteryNum = new Random();

            var randoms = new List<int>();
            randoms.Add(lotteryNum.Next(1,4));
            var rand2 = lotteryNum.Next(1, 4);
            if (!randoms.Contains(rand2))
                randoms.Add(rand2);
            var rand3 = lotteryNum.Next(1, 4);
            if (!randoms.Contains(rand3))
                randoms.Add(rand3);

            //Code to display random numbers here

            //Compare the two lists. Since they both have distinct values respectively no worries of duplicate matches
            var matches = 0;
            if(input.Contains(randoms[0]))
                matches++;
            if (input.Contains(randoms[1]))
                matches++;
            if (input.Contains(randoms[2]))
                matches++;

            //Do logic for displaying matches to user
        }

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