第四次作业——四则运算初版
第四次作业:
作业要求来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2186
我的 github 地址:https://github.com/CJL29120/ruangong
一、需求分析
具体要求:任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。例如输入数字是 30, 那就输出 30 道题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。
二、功能设计
1、根据用户输入的数字自动产生相应数目的三项式四则运算式;
2、根据用户输入的答案,判断用户输入的答案是否正确;
3、程序所产生的符号和数字均为随机数字。
三、详细设计
1、数据处理
试题类:
getExercise:产生随机试题;
getAnswer:用于检验随机试题是否符合标准;
test:测试用---显示试题,试题答案;
import java.text.DecimalFormat;
import java.util.Random;
public class Exercise {
private String exercise = "出错!!!";
private String operator1 = "", operator2 = "";
private int data1 = -1,data2 = -1,data3 = -1;
private double answer = -1;
public String getExercise() {
boolean success = false;
exercise = "";
while(!success){
data1 = new Random().nextInt(100) + 1;
data2 = new Random().nextInt(100) + 1;
data3 = new Random().nextInt(100) + 1;
{
int op1 = new Random().nextInt(4) + 1;
int op2 = new Random().nextInt(4) + 1;
answer = data1;
switch (op1) {
case 1:
operator1 = "+";
answer += data2;
break;
case 2:
operator1 = "-";
answer -= data2;
break;
case 3:
operator1 = "*";
answer *= data2;
break;
case 4:
operator1 = "/";
answer /= data2;
break;
default:
operator1 = "";
}
switch (op2) {
case 1:
operator2 = "+";
break;
case 2:
operator2 = "-";
break;
case 3:
operator2 = "*";
break;
case 4:
operator2 = "/";
break;
}
}
if (getAnswer() > 0) {
exercise += String.valueOf((int) data1) + " " + operator1 + " " + String.valueOf((int)data2) + " " + operator2 + " " + String.valueOf((int)data3) + "=";
success = true;
}
}
return exercise;
}
public double getAnswer() {
answer = data1;
switch (operator1) {
case "+":
answer += data2;
break;
case "-":
answer -= data2;
break;
case "*":
answer *= data2;
break;
case "/":
answer /= data2;
break;
default:
break;
}
switch (operator2) {
case "+":
answer += data3;
break;
case "-":
answer -= data3;
break;
case "*":
answer *= data3;
break;
case "/":
answer /= data3;
break;
}
DecimalFormat df = new DecimalFormat("#.00");
answer = Double.parseDouble(df.format(answer));
return answer;
}
public void test(){
System.out.println("运算题:"+getExercise());
System.out.println("答案:"+getAnswer());
}
}
校验类:
testAndGetValue:用于校验输入字符是否包含"/",并返回数值。
import java.text.DecimalFormat;
public class Verify {
public double testAndGetValue(String str){
int position =-1;
int data1,data2;
double value ;
position = str.indexOf(".");
position = str.indexOf("/");
if(position > 0){
data1 = Integer.valueOf(str.substring(0,position));
data2 = Integer.valueOf(str.substring(position+1,str.length()));
DecimalFormat df = new DecimalFormat("#.00");
String b = df.format((double) data1 / (double) data2);
value = Double.parseDouble(b);
}else{
value = Double.valueOf(str);
}
return value;
}
}
主要实现类:
打印输出提示,
循环
调用试题类的随机出题函数,
提示输入答案,
调用校验类校验输入答案,
判断结果,计算得分
循环结束
打印输出得分
import java.util.Scanner;
public class main {
public static void main(String[] args) {
System.out.println("算术题正在生成中,请稍等...");
try {
Thread.currentThread().sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
Exercise exercise = new Exercise();
Verify verify = new Verify();
String input;
int score=0;
for (int i = 0; i < 30; i++) {
System.out.println((i+1) + ".\n" +exercise.getExercise());
System.out.println("请输入第" + (i+1) + "题答案:");
input = new Scanner(System.in).next();
/* System.out.println("answer:"+exercise.getAnswer());
System.out.println("input:"+verify.testAndGetValue(input)); */
if(exercise.getAnswer() == verify.testAndGetValue(input)){
System.out.println("True!\n");
score++;
}else{
System.out.println("false!\n");
}
}
System.out.println("得分为:" + score +"分");
}
}
四、运行结果
五、过程耗时估计统计表
PSP2.1 |
Personal Software Process Stages |
Time Senior Studen(h) |
Time(h) |
Planning |
计划 |
0.5 |
0.5 |
Estimate |
估计这个任务需要多少时间 |
2 |
4 |
Development |
开发 |
1.5 |
3 |
Analysis |
需求分析 |
0.1 |
0.5 |
Design Spec |
生成设计文档 |
0 |
0 |
Design Review |
设计复审 |
0.1 |
0.5 |
Coding Standard |
代码规范 |
0.1 |
0.5 |
Design |
具体设计 |
0.2 |
1 |
Coding |
具体编码 |
0.8 |
3 |
Code Review |
代码复审 |
0 |
0 |
Test |
测试(自我测试,修改代码,提交修改) |
0.5 |
0.5 |
Reporting |
报告 |
1 |
1 |
|
测试报告 |
0 |
0 |
|
计算工作量 |
1.8 |
3.2 |
|
并提出过程修改计划 |
0 |
0
|