结对编程——四则运算
一、问题描述:
某公司程序员二柱的小孩上了小学二年级,老师让家长每天出100道(100以内)四则运算题目给小学生做。
二、结对对象:周芳
博客地址:http://home.cnblogs.com/u/tiffanybear/
双方贡献:1:1
结对编程照片:
三、采用语言:java(代码实现如下)
1 package four_calculate; 2 3 public class Calculate { 4 5 public String randomNum() { 6 int dividend = (int) (Math.random() * 10);// 随机生成被除数 7 int divisor = (int) (Math.random() * 10);// 随机生成除数 8 if (dividend >= divisor) { 9 int temp = dividend; 10 dividend = divisor; 11 divisor = temp; 12 } 13 return ("(" + dividend + "/" + divisor + ")"); 14 } 15 16 public static void main(String[] args) { 17 18 String divFirstNum = null; 19 String divSecondNum = null; 20 21 int k; 22 int count = 100; 23 Calculate calculate = new Calculate(); 24 for (int n = 1; n <= count; n++) { 25 26 divFirstNum = calculate.randomNum(); 27 divSecondNum = calculate.randomNum(); 28 29 int first = (int) (Math.random() * 100);//算术表达式中的第一个数 30 int second = (int) (Math.random() * 100);//算术表达式中的第二个数 31 32 switch ((int) (Math.random() * 4)) { 33 34 case 0: 35 k = (int) (Math.random() * 3); 36 if (k == 0) 37 System.out.println(n +": "+ first + "+" + second + "="); 38 else if (k == 1) 39 System.out.println(n +": "+divFirstNum + "+" + second + "="); 40 else if (k == 2) 41 System.out.println(n +": "+divSecondNum + "+" + divFirstNum + "="); 42 break; 43 case 1: 44 k = (int) (Math.random() * 3); 45 if (first < second) { 46 int temp = first; 47 first= second; 48 second = temp; 49 } 50 if (k == 0) 51 System.out.println(n +": "+first + "-" + second + "="); 52 else if (k == 1) 53 System.out.println(n +": "+second + "-" + divFirstNum + "="); 54 else if (k == 2) 55 System.out.println(n +": "+divSecondNum + "-" + divFirstNum + "="); 56 break; 57 case 2: 58 k = (int) (Math.random() * 3); 59 if (k == 0) 60 System.out.println(n +": "+first + "*" + second + "="); 61 else if (k == 1) 62 System.out.println(n +": "+divFirstNum + "*" + second + "="); 63 else if (k == 2) 64 System.out.println(n +": "+divSecondNum + "*" + divFirstNum + "="); 65 break; 66 case 3: 67 while (second == 0) 68 second = (int) (Math.random() * 100); 69 System.out.println(n +": "+first + "/" + second + "="); 70 break; 71 } 72 } 73 } 74 75 }
四、运行结果截图:
五、心得体会:
通过这次结对编程,更深刻地体会到了结对编程的好处,也向队友学习到了许多,比如编程的严谨性,思路等。当然,通过结对编程思路也更清晰,出错次数也相对减少!希望以后能经常用这种好的编程方法互相学习!