PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 8 | |
? Estimate | ? 估计这个任务需要多少时间 | 10 | 8 |
Development | 开发 | ||
? Analysis | ? 需求分析 (包括学习新技术) | 30 | 15 |
? Design Spec | ? 生成设计文档 | 30 | 35 |
? Design Review | ? 设计复审 | 10 | 20 |
? Coding Standard | ? 代码规范 (为目前的开发制定合适的规范) | 20 | 20 |
? Design | ? 具体设计 | 20 | 20 |
? Coding | ? 具体编码 | 500 | 90 |
? Code Review | ? 代码复审 | 20 | 40 |
? Test | ? 测试(自我测试,修改代码,提交修改) | 200 | 150 |
Reporting | 报告 | ||
? Test Repor | ? 测试报告 | 40 | 20 |
? Size Measurement | ? 计算工作量 | 15 | 10 |
? Postmortem & Process Improvement Plan | ? 事后总结, 并提出过程改进计划 | 50 | 40 |
合计 | 476 |
只有加减法
只有正整数运算
没学负数
减法只会 :2位数减整10的数、2位数减小于等于10的数
比小学一年级多了乘除法
也是只会正整数运算
会2位数相加减的运算了
只会运算100以内的数
程序运行中、文件输出后要给明确的提示
写出具体实现的步骤
str1拼接到str尾部
查阅别人的博客发现,用cmd输入的参数的使用方法和字符串数组的使用方法一致。
原先我是用问号表达式来控制加减后面发现这种发放并不适用于2年级的加减乘除的情况。思考了一会儿,我将加减乘除法存入字符串数组当中,然后用随机数随机数组的下标就实现了,1年级随机数范围[0-1],2年级随机数范围[0-3]。
原先我打算用字符串来完成,但是后面突然想到,如果要进行大批的字符串拼接可变字符串的拼接效率是最高的,所有我改变使用可变字符串Stringbuffer来进行拼接,我用2个可变字符串分开拼接题目和答案,最后再把题目和答案拼接到一起就解决了这个问题。
百度查阅资料发现记事本里面的换行是‘\r\n‘
解决:我发现在主函数创建的可变字符串str在init()函数里面又重新创建了一次,删除init()函数里面的str可解决问题。
解决:txt文件的换行是‘\r\n‘,修改在字符串拼接过程中的换行符即可。
解决:将判断参数各种错误的代码独立抽出来写成一个check()函数,并且在出现错误的地方增加提示和正确的结束程序,防止程序抛错误。
/*
* init函数产生题目和答案
*/
public static void init(StringBuffer strbuf, String[] s) {
int result = 0; // 表示题目答案
int remainder = 0; // 表示余数
String operator = null; // 表示随机出来的运算符
String[] mark_code = { "+", "-", "*", "/" }; // 小学1年级和2年级所有的运算
StringBuffer strbuf1 = new StringBuffer(); // 用于答案的拼接
//for循环拼接题目和答案
for (int i = 1; i <= Integer.valueOf(s[0]); i++) {
//判断年级 判断该年级对应的运算
if (s.length == 1) {
operator = mark_code[(int) (Math.random() * 2)];
} else {
if (Integer.valueOf(s[1]) == 1) {
operator = mark_code[(int) (Math.random() * 2)];
} else if (Integer.valueOf(s[1]) == 2) {
operator = mark_code[(int) (Math.random() * 4)];
} else {
System.out.println("输入年级有误!!");
System.exit(0);
}
}
//题目序号
String number = "(" + i + ")" + " ";
// 生成2个随机数
int first = (int) (Math.random() * 100);
int second = (int) (Math.random() * 100);
// 按照年级需求生成指定的题目
if (operator.equals("+")) {
if (s.length == 2 && s[1].equals("2")) {
//二年级的加法题目答案不超过100
while (first + second > 100) {
first = (int) (Math.random() * 100);
second = (int) (Math.random() * 100);
}
} else {
while (first + second > 100) {
//1年级的加法:
//1. 答案不超过100
//2. 可以是2位数+个位数
//3. 可以是2位数+整的10位数
if (first % 2 == 0) {
first = (int) (Math.random() * 100);
second = (int) (Math.random() * 100);
if (second > 10)
second = second / 10;
} else {
first = (int) (Math.random() * 100);
if (first > 10)
second = second / 10 * 10;
second = (int) (Math.random() * 100);
}
}
}
result = first + second;
} else if (operator.equals("-")) {
// 控制左边的数必须大于右边的数。
if (first < second) {
int t;
t = first;
first = second;
second = t;
}
if (!(s.length == 2 && s[1].equals("2"))) {
//2年级的减法:
//1. 答案不能为负数
//2. 可以是2位数-个位数
//3. 可以是2位数-整的10位数
if (second > 10)second = second / 10 * 10;
result = first - second;
} else {
result = first - second;
}
} else if (operator.equals("*")) {
//控制乘法只能是99乘法表上面的
while (first > 10 || second > 10) {
first = (int) (Math.random() * 10);
second = (int) (Math.random() * 10);
}
result = first * second;
} else {
// 当运算符为除时候 除数不能为0
while (second == 0) {
second = (int) (Math.random() * 100);
}
//控制除数只能是小于等于10
while (second > 10) {
second /= 10;
}
if (first % second == 0) {
result = first / second;
} else { //控制不能整除将答案和余数都显示出来
if (first > second) {
result = (int) (first / second);
remainder = first - (int) (first / second) * second;
} else {
result = (int) (first / second);
remainder = second;
}
}
}
// 如果为除法要考虑是否有余数拼接在答案后面
if (operator.equals("/")) {
strbuf1.append(number + first + " " + operator + " " + second + " " + "=" + " " + result + "..." + remainder + "\r\n");
} else {
strbuf1.append(number + first + " " + operator + " " + second + " " + "=" + " " + result + "\r\n");
}
strbuf.append(number + first + " " + operator + " " + second + "\r\n");
}
// 将题目和答案拼接在strbuf可变字符串当中
strbuf = strbuf.append("\r\n" + strbuf1);
}
请给出本次实验使用的代码规范:
测试序号 | 具体输入 | 预期结果 | 实际结果 |
---|---|---|---|
1 | java MathExam6348 0 | 生成0道1年级题目 | 符合预期结果 |
2 | java MathExam6348 000000000098 | 生成98道1年级题目 | 符合预期结果 |
3 | java MathExam6348 0000000000112 2 | 生成112道2年级题目 | 符合预期结果 |
4 | java MathExam6348 123asd | 请输入一个正整数 | 符合预期结果 |
5 | java MathExam6348 123.5 | 请输入一个正整数 | 符合预期结果 |
6 | java MathExam6348 -123 | 请输入一个正整数 | 符合预期结果 |
7 | java MathExam6348 asdf56465 | 请输入一个正整数 | 符合预期结果 |
8 | java MathExam6348 1231654561 | 输入的数字过大 | 符合预期结果 |
9 | java MathExam6348 1231654561 2 | 输入的数字过大 | 符合预期结果 |
10 | java MathExam6348 -123 2 | 请输入一个正整数 | 符合预期结果 |
11 | java MathExam6348 | 输入参数的数量错误 | 符合预期结果 |
12 | java MathExam6348 501 1 | 生成501道1年级题目 | 符合预期结果 |
10 | java MathExam6348 456421231564 1 | 输入的数值过大 | 符合预期结果 |
还有就是每次写完一个小功能要及时交到github上面,做好注释这样才能方便以后的维护工作。
原文:https://www.cnblogs.com/zhou-shi-yuan-ISO8859-1/p/9671104.html