简单四则运算
Form1代码如下:
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 四则运算
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Count = 0;
public static int right = 0;
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int sum;
string a = label3.Text;
switch (a)
{
case "+":
sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
break;
case "-":
sum = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
break;
case "*":
sum = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
break;
default:
sum = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
break;
}
if (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == sum.ToString())
right++;
RandomNum();
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox3.Enabled = true;
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
label3.Text = "+";
}
private void button4_Click(object sender, EventArgs e)
{
label3.Text = "-";
}
private void button5_Click(object sender, EventArgs e)
{
label3.Text = "*";
}
private void button6_Click(object sender, EventArgs e)
{
label3.Text = "/";
}
private void button1_Click_1(object sender, EventArgs e)
{
RandomNum();
}
private void RandomNum()
{
Random random = new Random();
int number1, number2;
number1 = random.Next(1, 11);
number2 = random.Next(1, 11);
textBox1.Text = number1.ToString();
textBox2.Text = number2.ToString();
textBox3.Text = "";
Count++;
}
}
}
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 四则运算
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox1.Text = Form1.Count.ToString();
textBox2.Text = Form1.right.ToString();
textBox3.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";
}
}
}
总结:
看似简单,做起来却不是想象中的那么容易。改改删删的,也翻了点课本,问了同学,书到用时方知少啊,感觉还有很多要补习的。以前都是照着课本做的,这次不是了,还有点问题吧,在下面再看看。以后多练习这种吧,希望能学好编程,真正会用它来解决实际问题。