实验二 结对编程(阶段二)

一、实验目标

1、体验敏捷开发中的两人合作。

2、近一步提高个人编程技巧与实践。

 

 

 

二 、实验内容:

1)根据以下问题描述,练习结对编程(pair programming)实践;

2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。

3)要求在结对编程工作期间,两人的角色至少切换 4 次;

4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。

 

三、实验过程 

1、实验模式:

因为此电脑非本人,所以此次结对编程通过屏幕分享和远程控制来完成。所用语言为java。

2、实验代码流程图:

实验二 结对编程(阶段二)_第1张图片

 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、运行结果:实验二 结对编程(阶段二)_第2张图片

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); 
         }
}
 
 
 
四、实验小结
        通过本次结对编程实验,发现了许多在自己做实验的时候发现不到的问题,在两个人的积极讨论下解决了问题,这也让自己的动手能力何思考能力都能提高,
在结对编程中,在另一个人指导的情况下也会让自己在编程的过程中出现错误的几率大大降低,这是一个特别能提升自己能力的实验,也证明了1+1>2这个道理,
对我们以后的编程有着很大的意义。
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(实验二 结对编程(阶段二))