C#简单彩票计划

时间:2015-11-05 20:25:51

标签: c# winforms if-statement

只要所有3个数字不同,这样就可以正常工作,但如果用户输入2个或更多相同数字并且该数字与至少1个随机数相匹配,那么它会带有3个匹配结果($ 1000)

我该怎么做才能确保用户输入的2个或更多相同的号码是否会赢得3个匹配?

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

                }
            }


        }
    }
}

3 个答案:

答案 0 :(得分:2)

这是一种非常简化的方法:

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

它使用两个列表,一个用于用户输入的数字,另一个用于随机数。如果它们是重复的并不重要,它只能匹配一次。 lotteryNums.Remove将删除匹配号码的第一个实例,因此即使用户输入两次数字,也只会将其归功于一次匹配。

还要注意if语句是如何格式化的,以及阅读和遵循的更容易。尽量避免使用箭头代码,如果您的if语句超过2或3级,您可能需要重新考虑如何执行这些操作。

修改

如果您想计算匹配,非常简单,请更改以下内容:

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

?:被称为三元运算符,它就像一个评估为的短句if

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

然后您可以在if声明中使用它:

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!";

将来,当你可以使用循环时,你可以进一步简化它:

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

这使您可以灵活地制作具有不同数量的彩票系统而无需更改任何代码(除了确定奖金的if部分之外,但您也可以使用其他列表来解决这个问题或字典)。

答案 1 :(得分:0)

我建议填写两个Lists整数,一个用你的彩票号码,一个用户输入。然后你可以循环它们并计算匹配。如果您希望使用IndexOf()允许与位置无关的匹配,这也可以提供灵活性。

答案 2 :(得分:0)

您希望将输入整数和随机生成的整数分别存储在两个单独的列表中。然后,您需要在计算匹配数之前删除所有重复项。因此,对于您的示例,123和313将变为123和31,这将导致两个匹配。以下是关于如何使用if语句执行此操作的代码:

        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
        }