简体   繁体   English

获取随机卡并更改标签

[英]Get a random card and change the label

I'm just fooling around in Windows Forms and wish to click a button and then give a player 2 random cards, but when I click the button the label is empty. 我只是在Windows窗体中鬼混,希望点击一个按钮然后给玩家2张随机卡,但是当我点击按钮时标签是空的。 How do i pass the value to the string? 如何将值传递给字符串?

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

    private void DealTheCardsButton_Click(object sender, EventArgs e)
    {
        TheCards theCards = new TheCards();
        CardOneLabel.Text = theCards.card1;
    }
}

public class TheCards
{
    public TheCards()
    {
        Cards = new List<string>();
        Cards.Add("1");
        Cards.Add("2");
        Cards.Add("3");
        Cards.Add("4");
    }

    public List<string> Cards { get; set; }

    public string card1;
    public string card2;

    public string cardTest = "hej";

    public void GiveTwoRandomCardsFromCardsList()
    {
        Random random = new Random();
        int slumptal = random.Next(0, 4);
        card1 = Cards[1];

        bool flag = false;
        while (!flag)
        {
            Random random2 = new Random();
            int slumptal2 = random2.Next(0, 4);

            if (slumptal != slumptal2)
            {
                card2 = Cards[slumptal2];
                flag = true;
            }
        }
    }
}

If I call to change the CardOneLabel.Text to a string with a hardcoded value in it it works. 如果我打电话将CardOneLabel.Text更改为带有硬编码值的字符串,则可以正常工作。 It seems like the randomizing doesn't change the value of string card1 and string card2 . 似乎随机化不会改变string card1string card2的值。 As it is now, when I click on the button the label changes its value to nothing (empty). 就像现在一样,当我点击按钮时,标签将其值更改为空(空)。

How can i change the label value to a random card value? 如何将标签值更改为随机卡值?

But when I click the button the label is empty. 但是当我点击按钮时标签是空的。 How do i pass the value to the string? 如何将值传递给字符串?

When you try to assign the value 当您尝试分配值时

 CardOneLabel.Text = theCards.card1;

The constructor of that class TheCards is not initiating the card1 variable to anything, you are retrieving an empty string. 这个类的构造函数TheCards不启动card1变到什么,要检索一个空字符串。

The constructor below, is adding to the list Cards but nothing else is taking place.. Do you have additional code your not showing? 下面的构造函数,正在添加到列表Cards但没有其他任何事情发生..你有没有显示的其他代码?

public TheCards()
{
    Cards = new List<string>();
    Cards.Add("1");
    Cards.Add("2");
    Cards.Add("3");
    Cards.Add("4");
}

Your CardOneLabel is empty when you press the button because you are literally reading an empty string. 当你按下按钮时,你的CardOneLabel是空的,因为你真的在读一个空字符串。 Try calling your GiveTwoRandomCardsFromCardsList() method before assigning. 在分配之前尝试调用GiveTwoRandomCardsFromCardsList()方法。

You never call the GiveTwoRandomCardsFromCardsList method. 你永远不会调用GiveTwoRandomCardsFromCardsList方法。 Try this: 尝试这个:

private void DealTheCardsButton_Click(object sender, EventArgs e)
{
    TheCards theCards = new TheCards();
    theCards.GiveTwoRandomCardsFromCardsList();

    CardOneLabel.Text = theCards.card1;
}

By the way, don't create a new Random object for every random number you require. 顺便说一句,不要为您需要的每个随机数创建一个新的Random对象。 This might even give you the same random number due to how the pseudo-random number generator depends on the current time. 由于伪随机数发生器如何依赖于当前时间,这甚至可能会给出相同的随机数。 Just use the same Random instance over and over again. 只需反复使用相同的Random实例。 It will give you a new random number every time. 它每次都会给你一个新的随机数。

Also, you will sometimes get the same card twice. 此外,您有时会获得两次相同的卡。 I'll leave it to the OP to figure out how to always get two different cards, if that's the idea. 我会把它留给OP去弄清楚如何总是得到两张不同的牌,如果那是想法的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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