结对编程--四则运算
结对编程对象:张忠玉
1 package com.jkxy.yxj.arithmetic; 2 3 public class Arithmetic { 4 //产生分数 5 String fraction() { 6 int i = (int) (Math.random() * 10); 7 int j ; 8 do{ 9 j = (int) (Math.random() * 10); 10 }while (j == 0);//分母不能为0 11 return (i + "/" + j); 12 } 13 14 public static void main(String[] args) { 15 String fa, fb;//记录两个分数 16 int k;//确定两个数的构成,为两个都是分数,一个分数一个整数,两个都是整数 17 Arithmetic math = new Arithmetic(); 18 for (int n = 0; n < 100; n++) { 19 fa = math.fraction();//产生两个分数 20 fb = math.fraction(); 21 int i = (int) (Math.random() * 100); 22 int j = (int) (Math.random() * 100); 23 //随机产生0,1,2,3,分别代表加减乘除 24 switch ((int) (Math.random() * 4)) { 25 case 0: 26 k = (int) (Math.random() * 3); 27 if (k == 0) 28 System.out.println(i + "+" + j + "="); 29 else if (k == 1) 30 System.out.println(fa + "+" + j + "="); 31 else if (k == 2) 32 System.out.println(fb + "+" + fa + "="); 33 break; 34 case 1: 35 k = (int) (Math.random() * 3); 36 //小学生没有学过负数,所以在进行减法时,假如出现被减数大于减数,则将两数交换,再进行减法; 37 if (i < j) { 38 int temp = i; 39 i = j; 40 j = temp; 41 } 42 if (k == 0) 43 System.out.println(i + "-" + j + "="); 44 else if (k == 1) 45 System.out.println(j + "-" + fa + "="); 46 else if (k == 2) 47 System.out.println(fb + "-" + fa + "="); 48 break; 49 case 2: 50 k = (int) (Math.random() * 3); 51 if (k == 0) 52 System.out.println(i + "*" + j + "="); 53 else if (k == 1) 54 System.out.println(fa + "*" + j + "="); 55 else if (k == 2) 56 System.out.println(fb + "*" + fa + "="); 57 break; 58 case 3: 59 while (j == 0)//除数不能为0,假如除数为0,则再产生一个随机数 60 j = (int) (Math.random() * 100); 61 k = (int) (Math.random() * 3); 62 if (k == 0) 63 System.out.println(i + "/" + j + "="); 64 else if (k == 1) 65 System.out.println(fa + "/" + j + "="); 66 else if (k == 2) 67 System.out.println(fb + "/" + fa + "="); 68 break; 69 } 70 } 71 } 72 }
总结:
通过这次项目的实践,我深切体会到结对编程带来的好处,通过讨论以后发现,一起思考容易让问题得到更加全面的解决,而且相互激励能将问题解决的更快。这样的训练不仅可以提高我们与同学的沟通能力,而且能发现一些新的思维方式。