结对项目《四则运算》

学习进度条:

点滴成就 学习时间 新编写代码行数 博客量(篇) 学习知识点
第一周 10小时  0  0 了解软件工程
第二周 10小时  0  1  项目开题
第三周 15小时  0  1  开通博客、开展项目调查
第四周 20小时  200 1  需求文档、用例图、代码规范

第五周 15小时 0 0 软件工程
第六周 20小时 291 0 编译原理、软件工程详细设计
第七周 18小时 91 1 软件工程

1、编程问题

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

2、结对编程的出发点

为了体会结对编程的好处,和同伴一起相互学习,相互监督,相互促进,共同提高。

3、编程细节

(1). 结对编程对象:邹阳 2013110457

  对方博客地址:http://home.cnblogs.com/u/Master-zy/

   双方贡献比例: 1:1

 

(2). 源代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package textFactory;
 
/**
 * @function 检查合法性
 * @author zy
 *
 */
public class CheckClass {
    /**
     * @function 检查一个(int)数字是否符合要求
     * @param firstNum
     * @param maxNum
     * @param minNum
     * @return
     */
    public static boolean checkNumIsOkOrNot(int Num, int maxNum, int minNum) {
        boolean result = false;
        if (Num >= minNum && Num <= maxNum) {
            result = true;
        } else {
            result = false;
        }
        return result;
    }// end
 
    /**
     * @function 检查一个(double)数字是否符合要求
     * @param Num
     * @param maxNum
     * @param minNum
     * @return
     */
    public static boolean checkNumIsOkOrNot(double Num, int maxNum, int minNum) {
        boolean result = false;
        if (Num >= minNum && Num <= maxNum) {
            result = true;
        } else {
            result = false;
        }
        return result;
    }// end
 
    /**
     * @function 检查一个字符串是否为整数
     * @param value
     * @return
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }// end
 
    /**
     * @function检查运试算是否符合要求
     * @param firstNum
     * @param secondNum
     * @param maxNum
     * @param minNum
     * @param answer
     * @param symbol
     * @return
     */
    public static boolean checkTheTestIsOkOrNot(int firstNum, int secondNum,
            int maxNum, int minNum, double answer, char symbol) {
        boolean result = false;
        switch (symbol) {
        case '+':
            if (checkNumIsOkOrNot(answer, maxNum, minNum)) {
                result = true;
            } else {
                result = false;
            }// end if
            break;
        case '-':
            if (answer >= 0) {
                result = true;
            } else {
                result = false;
            }
            break;
        case '*':
            if (checkNumIsOkOrNot(answer, maxNum, minNum)) {
                result = true;
            } else {
                result = false;
            }// end if
            break;
        case '/':
            if (isInteger(String.valueOf(answer))) {
                result = true;
            } else {
                result = false;
            }// end if
            break;
        default:
            break;
        }// end switch
        return result;
    }// end
}// end class
 
 
package textFactory;
 
import java.util.ArrayList;
import java.util.Random;
 
public class ArithmeticTest {
    int fistNum;
    int seconeNum;
    double answer;
    char symbol;
    ArrayList calcultorSymbol = new ArrayList();
 
    public ArithmeticTest() {
        // TODO Auto-generated constructor stub
    }// end
 
    public ArithmeticTest(int fistNum, int seconeNum) {
        this.fistNum = fistNum;
        this.seconeNum = seconeNum;
    }// end
 
    public ArithmeticTest(int fistNum, int seconeNum, ArrayList calcultorSymbol) {
        super();
        this.fistNum = fistNum;
        this.seconeNum = seconeNum;
        this.calcultorSymbol = calcultorSymbol;
    }// end
 
    /**
     * @function 获取小于 i 的随机数
     * @param i
     * @return
     */
    public static int getRandomNum(int i) {
        Random random = new Random();
        int resNum;
        resNum = random.nextInt(i);
        return resNum;
    }// end
 
    /**
     * @function 随机获取一个运算符号
     * @return
     */
    public char getSymbol() {
        char symbol = '@';
        int index = getRandomNum(this.calcultorSymbol.size());
        symbol = (char) this.calcultorSymbol.get(index);
        return symbol;
    }// end
 
    /**
     * @function进行四则运算(无括号)
     * @param firstNum
     * @param secondNum
     * @param symbol
     */
    public void calcAnswer(int firstNum, int secondNum) {
 
        this.symbol = getSymbol();
        switch (symbol) {
        case '+':
            this.answer = firstNum + secondNum;
            break;
        case '-':
            this.answer = firstNum - secondNum;
            break;
        case '*':
            this.answer = firstNum * secondNum;
            break;
        case '/':
            this.answer = firstNum / secondNum;
            break;
        default:
            break;
        }// end switch
    }// end
 
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        String resultTest = fistNum + " " + this.symbol + " " + seconeNum
                + " =";
        return resultTest;
    }// end
 
}// end class

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package textFactory;
 
import java.util.ArrayList;
 
import org.omg.CORBA.TIMEOUT;
 
import staticProperty.StaticPropertyStudy;
 
/**
 * 算式测试与输出
 *
 * @author zy
 *
 */
public class Tester {
    public static void main(String[] args) {
        int testNum = 30;
        int maxNum = 101;
        int minNum = 0;
        int firstNum;
        int secondNum;
 
        /*
         * 建立操作符列表
         */
        ArrayList symbolList = new ArrayList();
        symbolList.add('+');
        symbolList.add('-');
        symbolList.add('*');
        symbolList.add('/');
 
        for (int i = 1; i <= testNum; i++) {
            boolean checkResult = true;
            while (checkResult) {
 
                ArithmeticTest arithmeticTest = null;
                firstNum = ArithmeticTest.getRandomNum(maxNum);
                secondNum = ArithmeticTest.getRandomNum(maxNum);
 
                // 创建一个运算式对象
                arithmeticTest = new ArithmeticTest(firstNum, secondNum,
                        symbolList);
                arithmeticTest.calcAnswer(arithmeticTest.fistNum,
                        arithmeticTest.seconeNum);
 
                checkResult = CheckClass.checkTheTestIsOkOrNot(firstNum,
                        secondNum, maxNum, minNum, arithmeticTest.answer,
                        arithmeticTest.symbol);
                if (checkResult) {
                    System.out.print("第 " + i + " 题: ");
                    System.out.println(arithmeticTest);
                    // System.out.println("答案是 --- >" + arithmeticTest.answer);
                    System.out.println();
                    checkResult = false;
                } else {
                    checkResult = true;
                }// end if
            }// end while
 
        }// end for
 
    }// end main
}// end class

  

复制代码

(3). 输入和输出

1)、没有答案的输出

 

2)、有答案的输出

 

 

posted @   嘿高莉萍  阅读(208)  评论(0编辑  收藏  举报
编辑推荐:
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
阅读排行:
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 《HelloGitHub》第 106 期
· 数据库服务器 SQL Server 版本升级公告
· 深入理解Mybatis分库分表执行原理
· 使用 Dify + LLM 构建精确任务处理应用
点击右上角即可分享
微信分享提示