自动出题算术训练
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 自动出题
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
init();
}
int a = 0;
int b = 0;
int result = 0;
int score = 0;
Boolean haveAddScore = false;
Random rnd = new Random();
private void init()
{
int choose = 0;
if (radioButton1.Checked)
{
choose = 1;
a = rnd.Next(11);
b = rnd.Next(11);
}
if (radioButton2.Checked)
{
choose = 2;
a = rnd.Next(21);
b = rnd.Next(21);
}
if (radioButton3.Checked)
{
choose = 3;
a = rnd.Next(8) + 1;
b = rnd.Next(8) + 1;
}
switch (choose)
{
case 1: addArSub(); break;
case 2: addArSub(); break;
case 3: chengfa(); break;
}
label1.Text = a.ToString();
label3.Text = b.ToString();
label6.Text = score.ToString() + "分";
label7.Text = "当前速度:" + (timer1.Interval / 1000) + "秒/题";
// 重置计数器
timer1.Stop();
timer1.Start();
}
// 加法
private void addArSub()
{
int op = rnd.Next(2);
if(op < 1)
{
label2.Text = "+";
result = a + b;
}
else
{
label2.Text = "-";
result = a - b;
}
}
// 乘法
private void chengfa()
{
label2.Text = "*";
result = a * b;
}
// 提交检查
private void button3_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
int f = 0;
if (input == "")
{
textBox1.BackColor = Color.Red;
}
else
{
f = int.Parse(input);
if (result == f)
{
textBox1.BackColor = Color.Green;
if (!haveAddScore)
{
score += 10;
haveAddScore = true;
}
label6.Text = score.ToString() + "分";
}
else
{
textBox1.BackColor = Color.Red;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
// 下一题
private void button4_Click(object sender, EventArgs e)
{
init();
textBox1.Text = "";
textBox1.BackColor = Color.White;
haveAddScore = false;
// 重置计数器
timer1.Stop();
timer1.Start();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.BackColor = Color.White;
haveAddScore = false;
init();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.BackColor = Color.White;
haveAddScore = false;
init();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.BackColor = Color.White;
haveAddScore = false;
init();
}
// 自动刷新题目
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
timer1.Start();
}
else
{
timer1.Stop();
}
}
// 加快
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Interval > 5000)
{
timer1.Interval -= 5000;
label7.Text = "当前速度:" + (timer1.Interval / 1000) + "秒/题";
}
}
// 减慢
private void button2_Click(object sender, EventArgs e)
{
if (timer1.Interval < 50000)
{
timer1.Interval += 5000;
label7.Text = "当前速度:" + (timer1.Interval / 1000) + "秒/题";
}
}
}
}
运行截图