第一次作业_四则运算题目生成程序
目录
一、作业信息
二、作业要求
三、代码提交
1.代码结构
2.代码说明
3.运行截图
四、个人小结
一、作业信息
| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18 |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/ahgc/AHPU-se-JSJ18/homework/11377 |
| 这个作业的目标 | 能对需求进行分析并实现;编码格式规范;学习博客撰写 |
| 学号 | 3180701302 |
二、作业要求
写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
(1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
(2)程序要求能处理用户的输入,判断对错,累积分数
(3)程序支持可以由用户自行选择加、减、乘、除运算
(4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
三、代码提交
1.代码结构
//随机产生一个分数或者整数 public void createFenShu(RandomN f) { Random random=new Random(); int a=random.nextInt(101); int b=random.nextInt(100) + 1; //产生随机数1-100作为分母 if(b!=0&&((a%b==0)||a<b)) //为整数或者真分数(分母不为0且分子小于分母) { f.setFenzi(a); f.setFenmu(b); } else createFenShu(f); }
` public void createZhengShu(RandomN f)
{
Random random=new Random();
int a=random.nextInt(101);
f.setFenzi(a);
f.setFenmu(1); //分母为1,即产生整数
}
@Override
public String toString()
{
// TODO Auto-generated method stub
int n = this.FindZuiDa();
int fenzi = this.fenzi/n;
int fenmu = this.fenmu/n;
if(fenzi == 0)
{
return Integer.toString(fenzi);
}
if(fenzi >= fenmu) //如果分子大于分母
{
if(fenzi % fenmu == 0){
return Integer.toString(fenzi / fenmu);
}
else
{
return Integer.toString(fenzi) + "/" + Integer.toString(fenmu);
}
}
//加运算
public String Add(RandomN r2)
{
int zuiXiao =this.FindZuixiao(r2);
int fenzi = ((zuiXiao / this.fenmu) * this.fenzi) + ((zuiXiao / r2.getFenmu())* r2.getFenzi());
this.setFenzi(fenzi);
this.setFenmu(zuiXiao);
return this.toString();
}
//减运算
public String Substract(RandomN r2)
{
int zuiXiao =this.FindZuixiao(r2);
int fenzi = ((zuiXiao / this.fenmu) * this.fenzi) - ((zuiXiao / r2.getFenmu())* r2.getFenzi());
this.setFenzi(fenzi);
this.setFenmu(zuiXiao);
return this.toString();
}
//乘法运算:
public String mul(RandomN r2)
{
this.fenzi = this.fenzi * r2.getFenzi();
this.fenmu = this.fenmu * r2.getFenmu();
return this.toString();
}
//除运算
public String deivde(RandomN r2)
{
this.fenzi = this.fenzi * r2.getFenmu();
this.fenmu = this.fenmu * r2.getFenzi();
return this.toString();
}
2.代码说明
(1)用一个随机数生成两个随机数,使得这两个数是整数或者分数随机。
int totalcot = 0; //总题数
int cnt = 0; //当前已完成的题数,用来做循环的判断
int rightcot = 0; //做正确的题数
int errorCnt = 0; //做错误的题数
Scanner input = new Scanner(System.in);
System.out.print("请输入你要做的题数:");
totalcot = input.nextInt();
while (cnt < totalcot)
{
RandomN a = new RandomN();
RandomN b = new RandomN();
int number = new Random().nextInt(2);
switch (number) //随机生成的题目是整式或者分式或者混合式子
{
case 0:
a.createZhengShu(a);
b.createZhengShu(b);
break;
case 1:
a.createFenShu(a);
b.createFenShu(b);
break;
default:
a.createZhengShu(a);
b.createFenShu(b);
break;
}
}
(2)用一个随机数产生加减乘除算式中的任意一个并验证用户输入的答案是否与正确答案一致,将分子、分母分别放在stra[0],Str[1]中。
number = new Random().nextInt(4);
String result ;
String[] stra = new String[2]; //存放用户输入的答案的分子和分母
switch (number) {
// 加法
case 0:
System.out.print(a.toString() + " + " + b.toString() + " = ");
result = input.nextLine();
while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
result = input.nextLine();
}
if (result.trim().indexOf("/") >= 0) { //如果输入的答案是分数
stra = result.trim().split("/"); //将输入答案的分子分母分开放在stra[0]和stra[1]中并去掉空格
} else {
stra[0] = result.trim();
stra[1] = "1"; //否则置分母为1
}
if (stra[1].equals("0")) { //若输入的分数分母为0,则答案错误
System.out.println("答案错误!正确答案为:" + a.Add(b));
errorCnt++;
} else {
try {
int fz1 = Integer.parseInt(stra[0].trim());
int fm1 = Integer.parseInt(stra[1].trim());
RandomN c = new RandomN(fz1, fm1);
a.Add(b);
if (c.toString().equals(a.toString())) {
rightcot++;
System.out.println("答案正确!");
} else {
errorCnt++;
System.out.println("答案错误!正确答案为:" + a.toString());
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
errorCnt++;
System.out.println("答案错误!正确答案为:" + a.Add(b));
}
}
break;
// 减法
case 1:
double x = (double) a.getFenzi() / a.getFenmu();
double y = (double) b.getFenzi() / b.getFenmu();
if (x < y) { //若出现负数,则将减数和被减数替换位置
int fzTemp = a.getFenzi();
int fmTemp = a.getFenmu();
a.setFenmu(b.getFenmu());
a.setFenzi(b.getFenzi());
b.setFenmu(fmTemp);
b.setFenzi(fzTemp);
}
(3)统计正确题数、错误题数以及正确率和错误率
System.out.println('\n'+"共答题"+cnt+"道,错误"+errorCnt+"题"+",正确"+rightcot+"题");
System.out.println("正确率为:"+rightcot*100/cnt+"%");
`
3.运行截图
1.
2.
3.
4.
5.
6.
7.
8.
四、个人小结
| psp2.1 | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
|---|---|---|---|
| Planning | 计划 | 10 | 30 |
| Estimate | 估计这个任务需要多少时间,并规划大致工作步骤 | 5 | 10 |
| Development | 开发 | 120 | 360 |
| Analysis | 需求分析(包括学习新技术 | 10 | 30 |
| Design Spec | 生成设计文档 | 15 | 40 |
| Design Review | 设计复审 | 5 | 5 |
| Coding Standard | 代码规范 | 3 | 3 |
| Design | 具体设计 | 10 | 50 |
| Coding | 具体编码 | 100 | 300 |
| Code Review | 代码复审 | 5 | 10 |
| Test | 测试(自我测试,修改代码,提交修改) | 10 | 10 |
| Reporting | 报告 | 10 | 10 |
| Test Report | 测试报告 | 3 | 2 |
| Size Measurement | 计算工作量 | 3 | 3 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 3 | 3 |

浙公网安备 33010602011771号