四则运算
第一个窗体代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace 四则运算 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 public static int Count = 0; 19 public static int Right = 0; 20 private void FF() 21 { 22 Random suiji = new Random(); 23 int a, b; 24 a = suiji.Next(0, 11); 25 b = suiji.Next(0, 11); 26 textBox1.Text = a.ToString(); 27 textBox2.Text = b.ToString(); 28 textBox3.Text = ""; 29 } 30 private void button1_Click(object sender, EventArgs e) 31 { 32 label1.Text = button1.Text; 33 FF(); 34 } 35 36 private void button2_Click(object sender, EventArgs e) 37 { 38 label1.Text = button2.Text; 39 FF(); 40 } 41 42 private void button3_Click(object sender, EventArgs e) 43 { 44 label1.Text = button3.Text; 45 FF(); 46 } 47 48 private void button4_Click(object sender, EventArgs e) 49 { 50 label1.Text = button4.Text; 51 FF(); 52 } 53 54 private void textBox3_KeyDown(object sender, KeyEventArgs e) 55 { 56 int result = 0; 57 string ff = label1.Text; 58 59 switch (ff) 60 { 61 case "+": 62 result = int.Parse(textBox1.Text) + int.Parse(textBox2.Text); 63 break; 64 case "-": 65 result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); 66 break; 67 case "×": 68 result = int.Parse(textBox1.Text) * int.Parse(textBox2.Text); 69 break; 70 case "÷": 71 result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); 72 break; 73 } 74 if (e.KeyCode == Keys.Enter) 75 { 76 if (textBox3.Text == result.ToString()) 77 { 78 Right++; 79 Count++; 80 MessageBox.Show("回答正确!"); 81 } 82 else 83 { 84 MessageBox.Show("回答错误!"); 85 FF(); 86 Count++; 87 } 88 FF(); 89 } 90 } 91 92 private void button5_Click(object sender, EventArgs e) 93 { 94 Form2 frm2 = new Form2(); 95 frm2.ShowDialog(); 96 } 97 } 98 }
第二个窗体代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace 四则运算 11 { 12 public partial class Form2 : Form 13 { 14 public Form2() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form2_Load(object sender, EventArgs e) 20 { 21 textBox1.Text = Form1.Count.ToString(); 22 textBox2.Text = Form1.Right.ToString(); 23 textBox3.Text = (Form1.Count - Form1.Right).ToString(); 24 } 25 } 26 }
运行截图:
总结:
这是我第二次做作业,做的不好,请老师指教!在我室友的帮助下我艰难的完成了这次作业,下一次我尽量独自完成老师布置的作业,我学的不是很好,通过这次的作业我确实学到了东西,我觉的有收获就很好。一步一步来吧。这次作业交的有点晚,希望老师能够见谅。
疑问:
那个除法的时候,经常会出现分母为 零,这个时候运行就会出错,不知道怎么处理这一个!