代码实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Chapter7_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public static int count = 0;//题目总数
public static int right = 0;//正确的题目总数
private void button1_Click(object sender, EventArgs e)
{
Random();
}
//产生1-10的随机数
private void Random()
{
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 = label2.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++;
Random();
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox3.Enabled = false;
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
label2.Text = "+";
Random();
}
private void button4_Click(object sender, EventArgs e)
{
label2.Text = "-";
Random();
}
private void button5_Click(object sender, EventArgs e)
{
label2.Text =" *";
Random();
}
private void button6_Click(object sender, EventArgs e)
{
label2.Text = "/";
Random();
}
}
}
//Form2窗体代码编写
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Chapter7_8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.count.ToString();
textBox2.Text = Form1.right.ToString();
textBox3.Text = ((Form1.right / (double)(Form1.count)) * 100).ToString() + "%";
}
计算器样式: