1

I am creating a random number generator, when a certain number is generated I want the label to change change colour.

Random RandomClass = new Random ();

Num1.Text = RandomClass.Next (1, 49).ToString();

if (Num1.Text >= "9") 
{
   this.Num1.BackColor = System.Drawing.Color.DarkBlue;
}

Now I know >= "9" Doesn't work but I can't seem to think of anything else.

4

2 回答 2

0

嗯,我想这会做到:

int r = RandomClass.Next (1, 49);
Num1.Text = r.ToString();

if (r >= 9) 
{
   this.Num1.BackColor = System.Drawing.Color.DarkBlue;
}
于 2013-03-17T20:02:19.237 回答
0

好吧,尚不清楚您真正想要什么,但您所做的是字符串比较而不是整数比较

如果你想比较 int 值,你可以这样做;

Random RandomClass = new Random ();
int i = RandomClass.Next (1, 49);
Num1.Text = i.ToString();

if (i >= 9) 
{
   this.Num1.BackColor = System.Drawing.Color.DarkBlue;
}
于 2013-03-17T20:09:29.983 回答