簡體   English   中英

C#簡單抽獎程序

[英]C# simple Lottery Program

只要所有 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.";
                        }
                    }

                }
            }


        }
    }
}

這是一個非常簡單的方法來做到這一點:

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部分,但您也可以使用另一個列表或字典來解決這個問題)。

我建議填寫兩個整數列表,一個是您的彩票號碼,另一個是用戶輸入。 然后你可以遍歷它們並計算匹配。 如果您想通過使用IndexOf()允許與位置無關的匹配,這也將提供靈活性。

您希望將輸入整數和隨機生成的整數分別存儲在兩個單獨的列表中。 然后您想在計算匹配數之前刪除任何重復項。 因此,對於您的示例,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
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM