一,需求设计
需要自动生成四则运算(+ - * /),运算结果也在100以内,剔除重复算式,生成的运算题存储到外部文件result.txt中
二,功能设计
基本功能:自动生成10道100以内的2个操作数的四则运算算式(+ - * /),要求运算结果也在100以内
扩展功能:剔除重复算式,2 + 3 = 和 2 + 3 = 是重复算式 2 + 3 = 和 3 + 2 = 不属于重复算式
题目数量可定制
相关参数可控制 :是否包含乘法和除法
操作数数值范围可控
操作数是否含负数
三,设计实现
System.out.println("请输入题目的数量:");
scanner=new Scanner(System.in);
x=scanner.nextInt();
int rigthanswer[]=new int [x];
int youranswer[]=new int [x];
System.out.println("请输入数值的最大范围:");
y=scanner.nextInt();
for(i=0;i<x;i++){
int a=(int)(Math.random()*y);//随机生成一个整数
int b=(int)(Math.random()*y);//随机生成一个整数
int c=(int)(Math.random()*3);//随机生成一个0-3的整数,生成运算符
if(c==0)
{//加法
d1=a+b;
System.out.print(a+"+"+b+"=");
}
if(c==1)
{//减法
if(a>=b){
d1=a-b;
System.out.print(a+"-"+b+"=");
}else {d1=b-a;
System.out.print(b+"-"+a+"=");
}
}
if(c==2)
{//乘法
d1=a*b;
System.out.print(a+"*"+b+"=");
}
if(c==3)
{//除法
d1=a/b;
System.out.print(a+"/"+b+"=");
}
System.out.println("请输入你的答案:");
q=scanner.nextInt();
youranswer[i]=q;
rigthanswer[i]=d1;
}
System.out.print("\n");
System.out.println("显示答案请输:1");
if(scanner.nextInt()==1){
System.out.println("正确答案:\n");
四,测试运行
请选择式子类型: 1、整数,2、分数 1 请输入题目的数量: 5 请输入数值的最大范围: 30 13+2=请输入你的答案: 15 13-3=请输入你的答案: 10 24+7=请输入你的答案: 32 25+14=请输入你的答案: 39 27-11=请输入你的答案: 16 显示答案请输: 1 1 正确答案: 15 10 31 39 16 答对4题答错1题 请选择式子类型: 1、整数,2、分数 2 请输入题目的数量: 5 请输入分母数值的最大范围: 100 17/40+2/99=请输入你的答案: 123/ 253 11/31-51/72=请输入你的答案: 34/64 14/19-13/20=请输入你的答案: 11/12 66/ 76+75/100=请输入你的答案: 345/456 28/40*30/ 64=请输入你的答案: 123/456 显示答案请输入:1 1 1763/3960 -789/2232 33/ 380 123/ 76 21/64 答对0题答错5题
五,代码片段
1.计算作对的题目与做错的题
if(scanner.nextInt()==1){
for(i=0;i<x;i++){
System.out.print(rigthanswer[i]);
System.out.println("\t");
if(youranswer[i]==rigthanswer[i]){
rigthN++;//计算做对的题目
}else{
wrongN++;//计算做错的题目
}
}
System.out.print("答对"+rigthN+"题");
System.out.print("答错"+wrongN+"题");
}
}
}
2.随机生成整数
if(scanner.nextInt()==1){
for(i=0;i<x;i++){
System.out.print(rigthanswer[i]);
System.out.println("\t");
if(youranswer[i]==rigthanswer[i]){
rigthN++;//计算做对的题目
}else{
wrongN++;//计算做错的题目
}
}
System.out.print("答对"+rigthN+"题");
System.out.print("答错"+wrongN+"题");
}
}
}
3.四则运算
if(c==0){ //加法
Z=x1*m2+x2*m1;
M=m1*m2;
d=simplification(Z,M);
System.out.print(x1+"/"+m1+"+"+x2+"/"+m2+"=");
}
if(c==1){ //减法
Z=x1*m2-x2*m1;
M=m1*m2;
d=simplification(Z,M);
System.out.print(x1+"/"+m1+"-"+x2+"/"+m2+"=");
}
if(c==2){ //乘法
Z=x1*x2;
M=m1*m2;
d=simplification(Z,M);
System.out.print(x1+"/"+m1+"*"+x2+"/"+m2+"=");
}
if(c==3){ //除法
Z=m1*x2;
M=m2*x1;
d=simplification(Z,M);
六,总结
因为自己没有基础,所以好多地方都不会,问了同学一点点完成的,还有很多等待我学习
七、 尝试撰写PSP
| PSP2.1 | 任务内容 | 计划共完成需要的时间(h) | 实际完成需要的时间(h) |
| Planning | 计划 | 8 | 48 |
| Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 8 | 48 |
| Development | 开发 | 4 | 24 |
| Analysis | 需求分析 (包括学习新技术) | 30min | 30min |
| Design Spec | 生成设计文档 | 0 | 0 |
| Design Review | · 设计复审 (和同事审核设计文档) | 0 | 0 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 0 | 0 |
| Design | 具体设计 | 30min | 30min |
| Coding | 具体编码 | 8 | 28 |
| Code Review | 代码复审 | 3 | 8 |
| Test | 测试(自我测试,修改代码,提交修改) | 1 | 2 |
| Reporting | 报告 | 1 | 30min |
| Test Report | 测试报告 | 30min | 50min |
原文:https://www.cnblogs.com/conghui19/p/11528529.html