一、具体代码
类代码
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace szys { class Class2 { public int Sum; public int Answer { get { return Sum; } } public int Max(int n1,int n2,char yunsuanfu) { if (yunsuanfu == ‘+‘) { return Sum = n1 + n2; } else if (yunsuanfu == ‘-‘) { if (n1 > n2) { return Sum = n1 - n2; } else { MessageBox.Show("第一个数要大于第二个数!"); } } else if (yunsuanfu == ‘/‘) { if (n2 == 0 || n2 < 0) { MessageBox.Show("分母不能为0且大于0!"); } else { return Sum = n1 / n2; } } else if (yunsuanfu == ‘*‘) { return Sum = n1 * n2; } return Sum; } } }
Form1代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace szys { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int right = 0; public static int Count = 0; private int t; private void button5_Click(object sender, EventArgs e) { string strCon = @"Data Source=.;Initial Catalog=jisuanqi;Integrated Security=True"; SqlConnection sqlcon = new SqlConnection(strCon); try { sqlcon.Open(); string slt = @"insert into jisuan(Number1,Operator,Number2)" + " values(‘" + textBox1.Text + "‘,‘" + textBox4.Text + "‘,‘" + textBox2.Text + "‘)"; SqlCommand cmd = new SqlCommand(slt, sqlcon); cmd.ExecuteNonQuery(); MessageBox.Show("出题成功!"); } catch (Exception ex) { MessageBox.Show("出题失败"+ex.ToString()); } sqlcon.Close(); textBox1.Clear(); textBox4.Clear(); textBox2.Clear(); } private void button2_Click(object sender, EventArgs e) { textBox1.Enabled = false; textBox2.Enabled = false; textBox4.Enabled = false; textBox1.BackColor = Color.Yellow; textBox4.BackColor = Color.Yellow; textBox2.BackColor = Color.Yellow; textBox3.BackColor = Color.Yellow; string strCon = @"Data Source=.;Initial Catalog=jisuanqi;Integrated Security=True"; SqlConnection sqlcon = new SqlConnection(strCon); int i = 1; sqlcon.Open(); string slt = @"select * from jisuan"; SqlCommand cmd = new SqlCommand(slt, sqlcon); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); DataTable dt = ds.Tables[0]; textBox1.Text = dt.Rows[i][0].ToString(); textBox4.Text = dt.Rows[i][1].ToString(); textBox2.Text = dt.Rows[i][2].ToString(); sqlcon.Close(); } private void textBox3_MouseClick(object sender, MouseEventArgs e)//计时; { label3.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { t = t + 1; label3.Text = t.ToString(); } private void button3_Click(object sender, EventArgs e) { MessageBox.Show("一共用时" + label3.Text + "秒"); Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void button4_Click(object sender, EventArgs e) { int a = int.Parse(textBox1.Text); int b = int.Parse(textBox2.Text); Char c = Convert.ToChar(textBox4.Text.Trim()); Class2 con = new Class2(); con.Max(a, b, c); if (con.Answer == int.Parse(textBox3.Text)) { MessageBox.Show("恭喜你,小朋友!回答正确!"); right++; Count++; } else { MessageBox.Show("回答错误,继续加油!小朋友"); Count++; } textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } } }
Form2代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace szys { 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(); textBox4.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%"; } } }
二、测试
三、总结
用数据库对已出算式题的保存打开就是对数据库的增加查询,首先建一个表,列名有第一个数,运算符,第二个数,然后编写增加查询语句。
时间: 2024-10-20 04:15:36