Random问题 namespace WindowsApplication2 { public partial class Form1 : Form { Random aa = new Random(); Random bb = new Random(); int cc = aa.Next(0, 100); int dd = bb.Next(0, 100); private void button2_Click(object sender, EventArgs e) { //int cc = aa.Next(0, 100); //int dd = bb.Next(0, 100); textBox1.Text = Convert.ToString(cc); textBox2.Text = Convert.ToString(dd);
为什么呢?? 好像在 private void button2_Click(object sender, EventArgs e) { Random aa = new Random(); Random bb = new Random(); int cc = aa.Next(0, 100); int dd = bb.Next(0, 100); textBox1.Text = Convert.ToString(cc); textBox2.Text = Convert.ToString(dd);
} 就没有问题!! 怎会是呢?谢谢
------解决方案--------------------
namespace WindowsApplication2 { public partial class Form1 : Form { static Random aa = new Random(); static Random bb = new Random(); int cc = aa.Next(0, 100); int dd = bb.Next(0, 100); private void button2_Click(object sender, EventArgs e) { //int cc = aa.Next(0, 100); //int dd = bb.Next(0, 100); textBox1.Text = Convert.ToString(cc); textBox2.Text = Convert.ToString(dd);
private static Random rand = new Random(); int cc = aa.Next(0, 100); ... 具体查看 http://msdn.microsoft.com/zh-cn/library/2dx6wyd4.aspx
------解决方案-------------------- 声明和方法里面不一样
------解决方案-------------------- 因为它算是局部变量