结对编程
一.需求分析
1.先设置Form1窗体,向里面添加8个Lable控件,3个TextBox控件,2个Button控件.
2:在Form2窗体中添加3个Lable控件,3个TextBox控件.
二.代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Count = 0;
public static int right = 0;
private void button1_Click(object sender, EventArgs e)
{
RandomNum();
}
private void RandomNum()
{
Random ran = new Random();
int n1, n2;
n1 = ran.Next(1, 11);
n2 = ran.Next(1, 11);
textBox1.Text = n1.ToString();
textBox2.Text = n2.ToString();
textBox3.Text = "";
Count++;
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int Count;
string c = label9.Text;
switch (c)
{
case "+":
Count = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
break;
case "-":
Count = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
break;
case "x":
Count = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
break;
default:
Count = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
break
}
if (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == Count.ToString())
right++;
RandomNum();
}
}
private void label5_Click(object sender, EventArgs e)
{
label10.Text = "+";
RandomNum();
}
private void label6_Click(object sender, EventArgs e)
{
label10.Text = "-";
RandomNum();
}
private void label7_Click(object sender, EventArgs e)
{
label10.Text = "*";
RandomNum();
}
private void label8_Click(object sender, EventArgs e)
{
label10.Text = "/";
RandomNum();
}
private void button2_Click_1(object sender, EventArgs e)
{
textBox3.Enabled = false;
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
好友合作:夏静蕊 王书博