User requirements:

The program can receive user input integer answer, and judge right and wrong at the end of the program, the number of correct answer, wrong answer. Note: 0 — 10 integers are randomly generated users can choose one of the four operations users can end the program running, and display the statistical results. On this basis, do incremental development. Increment content: 1) Deal with user’s wrong input, such as input letters or symbols, etc., deal with the situation where the denominator is 0 in the division operation, and deal with the situation where the result is negative, to ensure that there is no negative number at the primary school level, such as 5-8=-3; 2) Users can set the countdown; 3) Users can set the range of random integers and the number of questions; 4) The user can choose which calculation type, such as addition, subtraction, multiplication and division, or choose one of the four calculations randomly generated by the software.

Design idea:

Add new functionality in addition to what was previously possible. Because our teacher Chen recommended the book “Da Hua Reconstruction” to us during the national Holiday, the reconstruction came to mind at the first time.

(1) Ensure that the level is primary school, no negative numbers, such as “5-8=-3” this situation. So WHAT I want to do, when I subtract, when I evaluate it, is I want to see if the second term is bigger than the first term. If it is larger than the first number, let it pop up a dialog box and let it generate a new pair of random numbers.

(2) The user can set the countdown, add a timer control on the form, and set the initial value to 60; That way, just click Start and the countdown will start.

(3) Users can set the range of random integers, so the first thing THAT comes to my mind is to set a range when giving random numbers, which is actually very simple. Drag out two text boxes, such as textBox4 and textBox5, and then convert the values of the two text boxes to int and have the Random() method write a question from the range given.



(4) As for the number of questions to be set, my partner Li Yanyan told me that I could add a text box to store the number of questions I want to answer, and judge that when the number of questions is equal to the preset number of questions, let it pop up Form2.



(5) The user can choose which calculation type, such as addition, subtraction, multiplication and division, or choose one of the four calculations randomly generated by the software. This feels a little more difficult, but I first store the four operations in an array, and then randomly select one operation from the array to display in Label3, so that I can switch(label3.text). As follows:

 

Code implementation:

Form1.cs

 

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 _Random 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 public static int Count = 0; 19 private int t = 60; 20 public static int right = 0; 21 22 private void button1_Click(object sender, EventArgs e) 23 { 24 label2.Text = t.ToString(); 25 timer1.Enabled = true; 26 timer1.Interval = 1000; 27 timer1.Start(); 28 } 29 30 private void RDN() 31 { 32 Random rd = new Random(); 33 int r1, r2; 34 if (textBox4.text == "" && textBox5.text == "") 35 {36 Messagebox.show (" please enter the value range!" ); 37 return; 38 } 39 r1 = rd.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); 40 r2 = rd.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); 41 textBox1.Text=r1.ToString(); 42 textBox2.Text=r2.ToString(); 43 string [] fuhao = new string [] {" + ", "-", "x", "present"}; 44 label3. Text = fuhao [rd. Next (0, 5)]; 45 int result = 0; Parse(textbox1.text) + int.parse (textbox2.text); 46 switch (label3.text) 47 {48 case "+": 49 result = int.parse (textbox1.text) + int.parse (textbox2.text); 50 return; 51 case "-" : 52 if (int.Parse(textBox1.Text) >= int.Parse(textBox2.Text)) 53 { 54 result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); 55} 56 else 57 {58 messagebox.show (" please press Enter for next question! This question is not included in the total number of answers!" ); 59 } 60 return; Parse(textBox1.text) * int.Parse(textbox2.text); 63 return; 64 case "÷": 64 if (textbox2.text == "0") 66 {67 messagebox.show (" 数 据 数 据 ", 数 据 数 据, 数 据 数 据 ); 68 } 69 else 70 { 71 result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); 72 } 73 return; 74 } 75 } 76 77 private void RandomNum() 78 { 79 Random ran=new Random(); 80 int n1,n2; 81 if (textBox4.Text==""&& textbox5. Text=="") 82 {83 messagebox. Show(" please enter the value range!" ); 84 return; 85 } 86 n1=ran.Next(int.Parse(textBox4.Text),int.Parse(textBox5.Text)); 87 n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); 88 textBox1.Text=n1.ToString(); 89 textBox2.Text=n2.ToString(); 90 textBox3.Text=""; 91 } 92 93 private void timer1_Tick(object sender, EventArgs e) 94 { 95 if (t <= 0) 96 { 97 timer1.Enabled = false; 98 textBox3.Enabled = false; 99 messagebox.show (" Time up!" ); 100 textBox3.Enabled = false; 101 Form2 frm2 = new Form2(); 102 frm2.ShowDialog(); 103 } 104 t = t - 1; 105 label2.Text = t.ToString(); 106 } 107 108 private void button2_Click(object sender, EventArgs e) 109 { 110 timer1.Stop(); 111 Form2 frm2 = new Form2(); 112 frm2.ShowDialog(); 113 } 114 115 private void textBox3_KeyDown(object sender, KeyEventArgs e) 116 { 117 int result = 0; 118 string s = label3.Text; 119 if (Count == int.Parse(textBox6.Text)) 120 { 121 Form2 frm2 = new Form2(); 122 frm2.ShowDialog(); Parse(textbox1.text) + int.parse (textbox2.text); 128 break; 129 case "-" : 130 if (int.Parse(textBox1.Text) >= int.Parse(textBox2.Text)) 131 { 132 result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); 133} 134 else 135 {136 messagebox.show (" please press Enter for next question! This question is not included in the total number of answers!" ); 137 } 138 break; Parse(textBox1.text) * int.Parse(textbox2.text); 141 break; 142 case "÷": 143 if (textbox2.text =="0") 144 {145 messagebox.show (" 数 据 数 据 ", 数 据 数 据, 数 据 数 据 ); 146 } 147 else 148 { 149 result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); 150 } 151 break; 152 } 153 if (e.KeyCode == Keys.Enter) 154 { 155 if (textBox3.Text == result.ToString()) 156 { 157 right++; 158 Count++; 159 Messagebox.show (" Correct answer!" ); 160 } 161 else 162 { 163 if (textBox2.Text=="0"||int.Parse(textBox1.Text)-int.Parse(textBox2.Text)<0) 164 { 165 RandomNum(); 166} 167 else 168 {169 messagebox.show (" error! ") ); 170 RandomNum(); 171 Count++; 172 } 173 } 174 RandomNum(); 175 } 176 } 177 178 private void button3_Click(object sender, EventArgs e) 179 { 180 label3.Text = button3.Text; 181 RandomNum(); 182 } 183 184 private void button4_Click(object sender, EventArgs e) 185 { 186 label3.Text = button4.Text; 187 RandomNum(); 188 } 189 190 private void button5_Click(object sender, EventArgs e) 191 { 192 label3.Text = button5.Text; 193 RandomNum(); 194 } 195 196 private void button6_Click(object sender, EventArgs e) 197 { 198 label3.Text = button6.Text; 199 RandomNum(); 200 } 201 202 private void button7_Click(object sender, EventArgs e) 203 {204 if (textBox4.Text == "" && textBox5.Text == "") 205 {206 messagebox. Show(" Please enter the value range!" ); 207 return; 208 } 209 else 210 { 211 for (int i = 0; i < int.Parse(textBox6.Text); i++) 212 { 213 RDN(); 214} 215} 216} 217} 218}Copy the code

 

Form2.cs

 

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 _Random 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} 27}Copy the code

** Operation process:

When you answer the questions, you give the range, you give the number of questions you want to answer, and if you do it randomly, you don’t have to click on the algorithm you want to use, you just click on random.

The program will automatically generate the algorithm. And when the time is up, it will automatically pop up the time and test results.



支那

When the second number is greater than the first in subtraction or the denominator is 0 in division:

**** if no value range is entered before the calculation, ****

Tips for correct or incorrect answers:

 

PSP Time analysis:

 

Pair programming summary: Note: We did 4 deltas together, 2 deltas each, I did: \

3) Users can set the range of random integers and the number of questions; 4) The user can choose which calculation type, such as addition, subtraction, multiplication and division, or choose one of the four calculations randomly generated by the software.

And before that. That is, during the National Day holiday, our teacher Chen recommended the book “Reconstruction of Big Words” to us.

I compiled some I think programming and the useful words: [every do a refactoring, modify a little code, and then submit, for a test system. If the function of the system still remain, as ever, refactoring is successful, continue to the next refactoring. If not, you have to restore back to refactor. 】 【 through safe and smooth reconstruction method to reconstruct our system first, make it can cope with the demand, and then add code, implement new requirements. This process is known as “two hats” : one is a refactoring without new features, one is to add new functions to realize the new requirements. Because of this, we are thinking about the moment when the design is ok.” \

[we would like to modify the software, plus ca change, four motivation is: 1. Add new functionality; 2. The original function has a BUG; 3. To improve the structure of the original program; 4. Optimize the performance of original system. The first and the second motive, is derived from the function needs of customers, and the fourth one is the non-functional requirements from customers.”

I thought it was really good. This time we do incremental, is not to add new functionality.

First of all, I would like to thank my partner Li Yanyan for completing the pair programming together.

In the pair programming, Li Yanyan and I first listed the design ideas together. Then according to the design idea, I was responsible for writing the program at the beginning, and she was responsible for watching me write, and when the program went wrong, she could analyze with me. She is more careful and precise. So WHENEVER she came up with a new idea, I would write it for her, and THEN I would watch to see if it was the right path. I feel good about it. It’s more fun than writing a program by myself, and I have someone to help me analyze it when things go wrong. Although it took so long to write the program this time, the efficiency is not too high, after all, my ability is also limited to do this. She also told me a word, there is no best, only better! Yeah, so keep improving!