结对编程体会

结对编程作业:某公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道(100以内)四则运算题目给小学生做。

采用语言 :java

结对对象 :黄佳明 2013110416  博客地址: http://www.cnblogs.com/ezreal2016/

双方贡献比例:1:1;

贴出代码:

package com.csdhsm.calculate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @author Han
 * @description Four less than one hundred operations
 */
public class Main {
    
    /**
     * @description main method
     * @param args
     */
    public static void main(String[] args) {
        
        //init the list of operation
        List<Operation> operations = new ArrayList<Operation>();
        
        //the length of operation
        int length = 0;
        
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要生成的四则运算的个数:");
        
        //input the length
        while(true){
            
            if(scanner.hasNextInt()){
                
                length = scanner.nextInt();
                
                if(length < 0){
                    
                    System.out.println("请输入一个大于0的整数");
                }else{
                    
                    break;
                }
            }else{

                System.out.println("请输入一个整数");
            }
        }
        
        //get the list of operations
        while(operations.size() < length){
            
            Operation operation = new Operation();
            
            //get a correct operation
            if(operation.expressionIsCorrect()){
                
                //add to operations 
                operations.add(operation);
            }
        }
        
        //print
        for(Operation operation : operations){
            
            System.out.println(operation);
        }
    }
}

/**
 * @description A expression like 3 + 5 = 8
 * @author Han
 */
class Operation{
    
    /**
     * @field addition symbol
     */
    private final String ADD = "+";
    /**
     * @field subtraction symbol
     */
    private final String SUB = "-";
    /**
     * @field multiplicative symbol
     */
    private final String MUL = "×";
    /**
     * @field division symbol
     */
    private final String DIV = "÷";
    
    /**
     * @field number one
     */
    private int number1;
    /**
     * @field number one
     */
    private int number2;

    /**
     * @field my symbol
     */
    private String symbol;
    
    /**
     * @field expression result
     */
    private int result;
    
    /**
     * @constructor init expression
     */
    public Operation() {
        
        Random random = new Random();
        int index = random.nextInt(4);//get a random number 0-3

        this.symbol = getSymbol(index);
        
        this.number1 = getNumber();//get a random number 0-100
        this.number2 = getNumber();
        this.result = setResult();//set the result
    }
    
    /**
     * @description check the expression correct
     */
    public boolean expressionIsCorrect(){
        
        //if the result > 100 or result < 0 ,the expression is not correct
        if(result > 100 || result < 0){
            
            return false;
        }
        
        return true;
    }
    
    /**
     * @description set the result
     * @return
     */
    private int setResult() {
        
        //symbol is "".throw the exception
        if(this.symbol.trim().equals("")){
            
            throw new RuntimeException("the symbol is """);
        }
        
        //set the result
        if(this.symbol.equals(ADD)){
            
            return (this.number1 + this.number2);
        }else if(this.symbol.equals(SUB)){
            
            //check the result is below 0
            if(this.number1 < this.number2){
                
                int temp = this.number1;
                this.number1 = this.number2;
                this.number2 = temp;
            }
            return (this.number1 - this.number2);
        }else if(this.symbol.equals(MUL)){
            
            return (this.number1 * this.number2);
        }else if(this.symbol.equals(DIV)){
            
            return (this.number1 / this.number2);
        }
        
        return 0;
    }

    /**
     * @description get a number 
     * @return in 0-100
     */
    private int getNumber() {
        
        Random random = new Random();
        int number = random.nextInt(100);//get a random number 0-100
        
        return number;
    }

    /**
     * @description get a symbol 
     * @param index   0-add ,1-sub ,2-mul ,3-div
     * @return
     */
    private String getSymbol(int index){
        
        switch (index) {
        case 0:
            return ADD;
        case 1:
            return SUB;
        case 2:
            return MUL;
        case 3:
            return DIV;
        default:
            throw new RuntimeException("Unknow Exception!");
        }
    }
    
    /**
     * @description get number one
     * @return
     */
    public int getNumber1() {
        return number1;
    }

    /**
     * @description get number two
     * @return
     */
    public int getNumber2() {
        return number2;
    }

    /**
     * @description get the symbol
     * @return
     */
    public String getSymbol() {
        return symbol;
    }

    /**
     * @description get the result
     * @return
     */
    public int getResult() {
        return result;
    }
    
    @Override
    public String toString() {
        
        return number1 + symbol + number2 + "=";
    }
}

点滴成就

学习时间

新编写代码行数

博客量(篇)

学到的知识点

第一周

3h

0

0

初步认识“软件工程”

第二周

3h

0

1

逐渐了解“软件工程”的思想

第三周

3h

0

1

制作并进行问卷调查

第四周

6h

200

2

需求分析与结对编程

原文地址:https://www.cnblogs.com/a294098789/p/5376608.html