一、实验目标
1、体验敏捷开发中的两人合作。
2、近一步提高个人编程技巧与实践。
二 、实验内容:
1)根据以下问题描述,练习结对编程(pair programming)实践;
2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。
3)要求在结对编程工作期间,两人的角色至少切换 4 次;
4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。
三、实验过程
1、实验模式:
因为此电脑非本人,所以此次结对编程通过屏幕分享和远程控制来完成。所用语言为java。
2、实验代码流程图:

3、具体工作
本次实验我和丁涛交换了4次,在确定了用java语言之后,我们就展开了代码部分的编程,第一次是创建变量以及编写随机数函数,第二次交换是创建一个四则运算函数,第三次交换是创建主函数,最后一次交换是优化代码以及出现的错误。
4、实验中出现的错误以及解决方法
1、在创建四则运算函数中,需要调用随机数函数,但是在调用的时候参数的数值调用不过来导致程序错误,解决方法:我们在头部讲随机数的变量定义为全局变量,这样就可以传递参数值。
2、在主函数中出现 cannot make a static reference to the non-static method create() from the type main,解决方法:创建一个pair-programming类的对象pair,重新调用该对象中的create()函数。
4、仓库地址:https://github.com/DTer1999/Pair-Programming-about-Four-Arithmetic-Operation。
5、运行结果:
6、实验代码:
import java.util.Random;
import java.util.Scanner; public class Pair_programming { public int a; public int b; public int n; /* * function:产生3个随机数,分别对应运算数1、运算数2、运算符 * error:random函数如何生成随机数 * write:MaLe * guide:DingTao * */ public void random() { Random rand = new Random(); a = (int)(Math.random()*100); b = (int)(Math.random()*100); n = rand.nextInt(3); //0-3,0代表+,1代表-,2代表*,3代表/ } /* * function:创建一个四则运算 * error:函数调用,无法传递参数,random(int ,int ,int)为形参,无法传递三个值 * solve:创建全局变量 * write:DingTao * guide:MaLe * */ public int create() { int answer; random(); switch(n) { case 0: while(a+b>100) {random();} answer=a+b; System.out.println(a+"+"+b+"="+"?"); //50+20=? break; case 1: while(a-b<0) {random();} answer=a-b; System.out.println(a+"-"+b+"="+"?"); //50-20=? break; case 2: while(a*b>100) {random();} answer=a*b; System.out.println(a+"*"+b+"="+"?"); break; case 3: while(b==0 || a%b!=0) {random();} answer=a/b; System.out.println(a+"/"+b+"="+"?"); //40/20=? break; default: answer=-1; } return answer; } /* * function:机器创建10道题目,用户输入答案,判定对错,给出正确数及分数。 * question:Cannot make a static reference to the non-static method create() from the type main * solve:创建一个Pair_programming类的对象pair,重新调用该对象中的create()函数。 * write:MaLe * guide:DingTao * * Solving problem: * write:DingTao * guide:Male * */ public static void main(String[] args) { Scanner in=new Scanner(System.in); int answersys,answerpeo,count=0,score=0; //count正确的题目数 Pair_programming pair = new Pair_programming(); for(int i=0;i<10;i++) { answersys=pair.create(); answerpeo=in.nextInt(); if(answerpeo==answersys) { System.out.println("true"); count++; score+=10; } else { System.out.println("false"); } } System.out.println("你做对了"+count+"题目,你的得分为"+score); }来源:https://www.cnblogs.com/male/p/12636330.html