需求分析:
1.先有七个文本框 显示输入得数七个文本框分别六个显示红色(1-33)和一个蓝色(1-16)的球号
给每个文本框设置只读属性
2.有一个按钮点击按钮获取随机的球号
3.再有七个文本框分别六个显示红色(1-33)和一个蓝色(1-16)的球号
4.再有一个按钮用来判断是否中奖
5.再有一个按钮清空数据
实现代码:
//先创建一个随机函数
Random r = new Random();
//定义一个数组
int[] a = new int[7];
private void Form1_Load(object sender, EventArgs e)
{
//计算窗口加载居中的位置
int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.Location = new Point(left, top);
//窗口加载的时候获取焦点
textBox1.TabIndex = 0;
}
//判断选择的球号
private void Btn_01_Click(object sender, EventArgs e)
{
if (textBox1.Text =="" && textBox15.Text == "" && textBox14.Text == "" && textBox13.Text == "" && textBox12.Text == "" && textBox11.Text == "" && textBox10.Text == "")
{
MessageBox.Show("输入的球号不能为空");
}
else
{
//设置只读属性ReadOnly,这里是bool类型
textBox1.ReadOnly = textBox15.ReadOnly = textBox14.ReadOnly = textBox13.ReadOnly = textBox12.ReadOnly = textBox11.ReadOnly = textBox10.ReadOnly = true;
}
}
string num=" ";
private void Button1_Click(object sender, EventArgs e)
{
//文本框里面显示随机的数字
for (int i = 0; i < a.Length; i++)
{
Random对象有一个“Next”无参随机正整数,取值范围是 0-2147483647,返回值为int类型 可以取到1但是取不到34
a[i] = r.Next(1, 34);
for (int k = 0; k < i; k++)
{
//如果两次循环的数值相等的话
if (a[i] == a[k])
{
//我们让它重新随机
i--;
}
}
}
a[0] = r.Next(1, 34);
a[1] = r.Next(1, 34);
a[2] = r.Next(1, 34);
a[3] = r.Next(1, 34);
a[4] = r.Next(1, 34);
a[5] = r.Next(1, 34);
a[6] = r.Next(1, 17);
foreach (var item in a)
{
// textBox2.Text是string类型,a[0]是int类型, 这里我们通过int.tostring()方法转成string类型
textBox3.Text = a[0].ToString();//红色球
textBox4.Text = a[1].ToString();//红色球
textBox5.Text = a[2].ToString();//红色球
textBox2.Text = a[3].ToString();//红色球
textBox6.Text = a[4].ToString();//红色球
textBox7.Text = a[5].ToString();//红色球
textBox8.Text = a[6].ToString();//蓝色球
//把获取的随机数显示在大的文本框中 \n表示的是换行 \r表示的回车
num = textBox3.Text + "," + textBox4.Text + "," + textBox5.Text + "," + textBox2.Text + "," + textBox6.Text + "," + textBox7.Text + " " + textBox8.Text ;
}
textBox9.Text += (num + "\r" + "\n");
// textBox9.Text += num ;
}
private void Button2_Click(object sender, EventArgs e)
{
//定义一个count表示中了几个奖
int count = 0;
//我们利用索引把每一项都遍历出来
for (int i = 0; i < a.Length-1; i++)
{
//我们输入的第一个数字和数组中前6个数字都比较一下
if (textBox1.Text == a[i].ToString())
{
textBox1.BackColor = Color.Pink;
//count=count+1 表示中了几个奖
count += 1;
}
}
for (int i = 0; i < a.Length-1 ; i++)
{
//我们输入的第一个数字和数组中前6个数字都比较一下
if (textBox15.Text == a[i].ToString())
{
textBox15.BackColor = Color.Pink;
count += 1;
}
}
for (int i = 0; i < a.Length-1 ; i++)
{
if (textBox14.Text == a[i].ToString())
{
textBox14.BackColor = Color.Pink;
count += 1;
}
}
for (int i = 0; i < a.Length-1 ; i++)
{
if (textBox13.Text == a[i].ToString())
{
textBox13.BackColor = Color.Pink;
count += 1;
}
}
for (int i = 0; i < a.Length-1 ; i++)
{
if (textBox12.Text == a[i].ToString())
{
textBox12.BackColor = Color.Pink;
count += 1;
}
}
for (int i = 0; i < a.Length-1 ; i++)
{
if (textBox11.Text == a[i].ToString())
{
textBox11.BackColor = Color.Pink;
count += 1;
}
}
//蓝色的球号作比较
if (textBox10.Text == a[6].ToString())
{
textBox10.BackColor = Color.Pink;
count += 1;
}
//设置通知弹框
MessageBox.Show("恭喜您中奖啦。中了" + count + "个数字");
}
private void Button3_Click(object sender, EventArgs e)
{
//我们把文本清空
textBox1.Text = "";
//把颜色变成原来的白色
textBox1.BackColor = Color.White;
textBox2.Text = "";
textBox2.BackColor = Color.White;
textBox3.Text = "";
textBox3.BackColor = Color.White;
textBox4.Text = "";
textBox4.BackColor = Color.White;
textBox5.Text = "";
textBox5.BackColor = Color.White;
textBox6.Text = "";
textBox6.BackColor = Color.White;
textBox7.Text = "";
textBox7.BackColor = Color.White;
textBox8.Text = "";
textBox8.BackColor = Color.White;
textBox9.Text = "";
textBox9.BackColor = Color.White;
textBox10.Text = "";
textBox10.BackColor = Color.White;
textBox11.Text = "";
textBox11.BackColor = Color.White;
textBox12.Text = "";
textBox12.BackColor = Color.White;
textBox13.Text = "";
textBox13.BackColor = Color.White;
textBox14.Text = "";
textBox14.BackColor = Color.White;
textBox15.Text = "";
textBox15.BackColor = Color.White;
//清空的时候我们让球号还能输入故设置不只读
textBox1.ReadOnly = textBox15.ReadOnly = textBox14.ReadOnly = textBox13.ReadOnly = textBox12.ReadOnly = textBox11.ReadOnly = textBox10.ReadOnly = false;
}
}
}
实现结果如下: