jeudi 5 novembre 2015

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.";
                        }
                    }

                }
            }


        }
    }
}

Aucun commentaire:

Enregistrer un commentaire