一.需求分析
代替人工出题,写程序能够随机出题,并能保存计算结果
二.功能设计
(1)能够出指定的题目数量,并且操作数及结果必须均在100以内。
(2)算式不能重复
(3)题数可指定
(4)可以控制相关参数:控制是否有乘除
控制是否有负数
(5)生成的运算题存储到外部文件result.txt中
三.设计实现
rows控制输出列
定义输入输出类对象
创建result.txt文件
输入输出函数
四.测试与运行
(测试)

(运行结果)

五.代码展示
public String[][] CreateQuestion(int num, boolean is_include_Multiplication_division, int size_operationNum,
boolean is_include_negative) {
int num1, num2, result;
int type;
int count = 0;
String[][] Question_Answer = new String[99999][2];
while (true) {
if (count >= num)
break;
if (is_include_negative) {
num1 = (int) ((Math.random() * size_operationNum) - 50);
num2 = (int) ((Math.random() * size_operationNum) - 50);
} else {
num1 = (int) (Math.random() * size_operationNum);
num2 = (int) (Math.random() * size_operationNum);
}
if (!is_include_negative)
if (num1 < 0 || num2 < 0)
continue;
type = 1 + (int) (Math.random() * 4);
if (type == 1) {
result = num1 + num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " + " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 2) {
result = num1 - num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " - " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 3 && is_include_Multiplication_division) {
result = num1 * num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " × " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 4 && is_include_Multiplication_division) {
Double result_;
if (num2 == 0)
continue;
result_ = (double) num1 / num2;
if (result_ < 100) {
Question_Answer[count][0] = "("+num1+")" + " ÷ " +"("+ num2+")" + " =";
Question_Answer[count][1] = String.format("%.2f", result_); // 精度控制
count++;
}
}
}
return Question_Answer;
}
/*
* rows控制输出列
*/
public void SaveToDisk(String[][] question_answer, int lines, int rows) {
// 定义输入输出类对象
OutputStream outputStream = null;
// 获取时间,用于生成随时间变化的文件名
LocalDate date = LocalDate.now();
// 创建result.txt文件
try {
outputStream = new FileOutputStream("./result" + date + ".txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
public static void main(String[] args) {
Question_ q = new Question_();
int num = 10;
int size_operationNum = 100;
boolean is_include_Multiplication_division = false;
boolean is_include_negative = false;
Scanner input = new Scanner(System.in);
int rows = 1;
System.out.print("请输入100~1000的自然数");
size_operationNum = input.nextInt();
System.out.print("请输入要产生的题数");
num = input.nextInt();
System.out.print("若有乘除输入1,若无乘除输入0");
int flag = input.nextInt();
if (flag == 1)
is_include_Multiplication_division = true;
else
is_include_Multiplication_division = false;
System.out.print("若有负数输入1,若无负数输入0");
flag = input.nextInt();
if (flag == 1)
is_include_negative = true;
else
is_include_negative = false;
System.out.print("打印行数(默认为1行):");
rows = input.nextInt();
String[][] question_answer = q.CreateQuestion(num, is_include_Multiplication_division, size_operationNum,
is_include_negative);
int size = q.Question_Size(question_answer);
六.总结
先实现基本要求,再逐步添加复杂的需求
七.PSP
|
PSP2.1 |
任务内容 |
计划共完成需要的时间(min) |
实际完成需要的时间(min) |
|
Planning |
计划 |
10 |
15 |
|
· Estimate |
· 估计这个任务需要多少时间,并规划大致工作步骤 |
10 |
15 |
|
Development |
开发 |
120 |
140 |
|
·· Analysis |
需求分析 (包括学习新技术) |
10 |
15 |
|
· Design Spec |
· 生成设计文档 |
5 |
10 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
5 |
10 |
|
· Coding Standard |
代码规范 (为目前的开发制定合适的规范) |
3 |
5 |
|
· Design |
具体设计 |
10 |
15 |
|
· Coding |
具体编码 |
30 |
40 |
|
· Code Review |
· 代码复审 |
7 |
15 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
13 |
21 |
|
Reporting |
报告 |
9 |
15 |
|
·· Test Report |
· 测试报告 |
3 |
5 |
|
· Size Measurement |
计算工作量 |
2 |
3 |
|
· Postmortem & Process Improvement Plan |
· 事后总结 ,并提出过程改进计划 |
3 |
3 |
原文:https://www.cnblogs.com/nuah/p/11522125.html