需求分析:能够根据用户对题目数量的需求,生成一定数量的包含整数和真分数的题目,并且能够生成答案,判断正误,统计正确率。
功能设计:
1.随机生成整数和真分数的四则运算题目;
2.能够处理用户的输入,并判断正误,统计正确率;
3.能够自动计算出正确答案;
设计实现:代码是用java写的,写了两个类,一个是zhenshu ,用于实现整数运算,另一个是zhenfenshu ,用于实现真分数运算,在zhenfenshu类中,有写了一个yuefen的函数,用于对计算结果进行约分。
源代码链接:https://git.coding.net/echo1/liucun.git
代码说明:
public static String yuefen(int a,int b){
int y = 1;
for(int i=a;i>=1;i--){
if(a%i==0&&b%i==0){
y = i;
break;
}
}
int z = a/y;
int m = b/y;
if(z==0) {
return "0";
}
return ""+z+"/"+m;
}
上面是真分数类里的yuefen()函数,用于对计算结果进行约分。
public static void main(String[] args){
int M,Z,x,i,n=0;
double y;
String d=null;
String e=null;
System.out.println("请输入题目的数量");
Scanner scan2=new Scanner(System.in);
x=scan2.nextInt();
String daan[]=new String [x];
int x1,x2,B,m1,m2;
System.out.println("请输入分母数值的范围");
B=scan2.nextInt();
for(i=0;i<x;i++){
m1=1+(int)(Math.random()*B);
x1=1+(int)(Math.random()*m1);
m2=1+(int)(Math.random()*B);
x2=1+(int)(Math.random()*m2);
int c=(int)(Math.random()*3);
if(c==0){
Z=x1*m2+x2*m1;
M=m1*m2;
d=yuefen(Z,M);
System.out.print(x1+"/"+m1+"+"+x2+"/"+m2+"= ");
}
if(c==1){
Z=x1*m2-x2*m1;
M=m1*m2;
d=yuefen(Z,M);
System.out.print(x1+"/"+m1+"-"+x2+"/"+m2+"= ");
}
if(c==2){
Z=x1*x2;
M=m1*m2;
d=d=yuefen(Z,M);
System.out.print(x1+"/"+m1+"*"+x2+"/"+m2+"= ");
}
if(c==3){
Z=m1*x2;
M=m2*x1;
d=d=yuefen(Z,M);
System.out.print(x1+"/"+m1+"/"+x2+"/"+m2+"= ");
}
daan[i]=d;
System.out.println("请输入答案");
e=scan2.next();
if(e.equals(d))
{
n++;
System.out.println("true"); //判断用户输入答案的正误
}
else
System.out.println("false");
}
y=(double)n/x; //统计正确率
System.out.println("是否显示答案(显示请输入0)");
if(scan2.nextInt()==0){
for(i=0;i<x;i++){
System.out.print(daan[i]+" "); //显示正确答案
}
System.out.println("正确率是"+y);
}
}
}
上面的是zhenfenshu类里的main()函数,用于实现真分数四则运算的一系列功能。
public class zhengshu {
public static void main(String[] args) {
int x, y, i,n=0,d=0,e=0;
double z;
Scanner scan1 = new Scanner(System.in);
System.out.println("请输入题目的数量");
Scanner scan2 = new Scanner(System.in);
x = scan2.nextInt();
int daan[] = new int[x];
System.out.println("请输入数值的范围");
y = scan2.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);
if (c == 0) {
d = a + b;
System.out.print(a + "+" + b + "= ");
}
if (c == 1) {
d = a - b;
System.out.print(a + "-" + b + "= ");
}
if (c == 2) {
d = a * b;
System.out.print(a + "*" + b + "= ");
}
if (c == 3) {
d = a / b;
System.out.print(a + "/" + b + "= ");
}
daan[i] = d;
System.out.println("请输入正确答案");
e=scan2.nextInt();
if(e==d)
{n++;
System.out.println("true");
}
else
System.out.println("false");
}
z=(double)(n/x);
System.out.println("是否显示答案(显示输入0)");
if (scan2.nextInt()==0){
for (i = 0; i < x; i++) {
System.out.print(daan[i] + " ");
}
}
System.out.println("正确率 "+z);
}}
上面是zhengshu类的代码,用于实现整数的四则运算。
运行截图
PSP:
PSP2.1
|
Personal Software Process Stages
|
Time(min) Senior Student
|
Time (min)
|
Planning
|
计划
|
5
|
5
|
Estimate
|
估计这个任务需要多少时间
|
5
|
3
|
Development
|
开发
|
20
|
30
|
Analysis
|
需求分析 (包括学习新技术)
|
3
|
20
|
·Design Spec
|
生成设计文档
|
5
|
3
|
·Design Review
|
设计复审
|
1
|
1
|
Coding Standard
|
代码规范
|
20
|
15
|
· Design
|
具体设计
|
15
|
10
|
Coding
|
具体编码
|
30
|
50
|
·Code Review
|
代码复审
|
10
|
10
|
Test
|
测试(自我测试,修改代码,提交修改)
|
15
|
20
|
Reporting
|
报告
|
10
|
10
|
|
测试报告
|
3
|
3
|
|
计算工作量
|
3
|
5
|
|
并提出过程改进计划
|
5
|
0
|
小结:刚开始看到题目的时候,觉得无从下手,不知道从哪开始做起,就百度了很多相似的题目,照着别人做过的代码进行修改,然后再一点一点满足这个题目的要求,最后运行,出错,然后修改,到最后做好,花了两天时间,现在来看,其实题目不是很难,只是自己之前动手写代码的时间太少了,拿到题目之后不知到怎么开始,希望自己以后能够多多练习,尽量少百度网上的代码,能够自己写出代码。
四则运算
原文:http://www.cnblogs.com/voga/p/6507083.html