作业
四则运算
--计应192 七组
一、需求分析:
任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。
二、功能设计:
1.根据用户要求的题目数n,循环n次输出随机的四则运算。
2.判断用户输入值是否正确,并给出正确题目数。
3.运算答案不能小于0,小于0就重新在后台出题目。
三.个人开发流程:
|
PSP阶段 |
预计花费时间(h) |
实际花费时间(h) |
|
计划(Planning) |
2 |
3 |
|
开发(Development) |
48 |
60 |
|
分析需求(Analysis) |
4 |
6 |
|
生成设计文档(Design Spec) |
4 |
6 |
|
设计复审(Design Review) |
2 |
4 |
|
规范代码(Coding Standard) |
2 |
3 |
|
具体设计(Design ) |
4 |
7 |
|
具体编码(Coding) |
8 |
12 |
|
代码复审(Code Review) |
4 |
7 |
|
测试(Test) |
2 |
3 |
|
测试报告(Test Report) |
2 |
2 |
|
事后总结(Postmortem) |
2 |
3 |
|
提出过程改进计划(Process Improvement Plan) |
2 |
4 |
四,代码实现:
1)主要运算
Public void operation(int n){
int op1,op2;
for (int i = 0; i < n; i++) {
num1 = new Random().nextInt(100)+1;
num2 = new Random().nextInt(100)+1;
num3 = new Random().nextInt(100)+1;
op1=new Random().nextInt(4)+1;
op2=new Random().nextInt(4)+1;
if(op1 == 1){
String str = num1 + "+" + num2;
if(op2 == 1){
result = add(num1,num2);
result = add(result,num3);
str = str + "+" + num3;/*++*/
}
else if(op2 == 2){
result = add(num1,num2);
while(result < num3){
num3 = new Random().nextInt(100)+1;
}
result = sub(result,num3);
str = str + "-" + num3;/*+-*/
}
else{
str = pri(op1,op2);
}
print(str);
}
else if(op1 == 2){
String str = num1 + "-" + num2;
String str = "";
if(op2 == 1){
result = sub(num1,num2);
while(Math.abs(result) > Math.abs(num3)){
num3 = new Random().nextInt(100)+1;
}
result = add(result,num3);
str = num1 + "-" + num2 + "+" + num3;
}
else if(op2 == 2){
while(num1 < num2){
num1=newRandom().nextInt(100)+1;
}
result = sub(num1,num2);
str = num1 + "-" + num2;
while(result < num3){
num3 = new Random().nextInt(100)+1;
}
result = sub(result,num3);
}
else{
str = pri(op1,op2);
}
print(str);
}
else if(op1 == 3){
String str = num1 + "*" + num2+"*" + num3;
result = mul(num1,num2);
result = mul(result,num3);
str = operation2(op1,op2,str);
print(str);
}
else if(op1 == 4){
String str = num1 + "÷" + num2;
result = div(num1,num2);
str = operation2(op1,op2,str);
print(str);
}
else {
System.out.println("出错了。。。。");
}
}
System.out.println(n+"题中"+"您一共答对了"+mark+"题。");
}
2)对先进行加(减)法再进行乘(除)法的算式进行优先级判断和处理
public String pri(int op1,int op2){
String str = "";
if(op2 == 3){
result = mul(num2,num3);
str = comPri(op1,op2,str);
str = str + "*" + num3;
}
if(op2 == 4){
result = div(num2,num3);
str = comPri(op1,op2,str);
str = str + "÷" + num3;
}
return str;
}
public String comPri(int op1,int op2,String str){
if(op1 == 1){
result = add(num1,result);
str = num1 + "+" + num2;
}
if(op1 == 2){
while(num1 < result){
num2 = new Random().nextInt(100)+1;
num3 = new Random().nextInt(100)+1;
result = mul(num2,num3);
}
result = sub(num1,result);
str = num1 + "-" + num2;
}
return str;
}
3)对除法中的运算进行分析
public String operation2(int op1,int op2,String str){
if(op2 == 1){
result = add(result,num3);
return str + "+" + num3;
}
else if(op2 == 2){
while(result < num3){
num1 = new Random().nextInt(100)+1;
num2 = new Random().nextInt(100)+1;
num1,num2*/
result = div(num1,num2);
str = num1 + "÷" + num2;
}
result = sub(result,num3);
return str + "-" + num3;
}
else if(op2 == 3){
result = mul(result,num3);
return str + "*" + num3;
}
else{
result = div(result,num3);
return str + "÷" + num3;
}
}
4)输出代码
public void print(String str){
Scanner s = new Scanner(System.in);
System.out.print(str + "=");
int input = s.nextInt();
if(input == result){
mark=mark+1;
System.out.println("回答正确!");
System.out.println("已答对"+mark+"题。");
}
else {
System.out.println("回答错误!正确答案为:" + str + "=" +result);
}
}
四则运算
--计应192 七组赵寒月
一、需求分析:
任何编程语言都可以,命令行程序接受一个数字输入,然后输出相应数目的四则运算题目和答案。 运算式子必须至少有两个运算符,运算数字是在 100 之内的正整数,答案不能是负数。
二、功能设计:
1.根据用户要求的题目数n,循环n次输出随机的四则运算。
2.判断用户输入值是否正确,并给出正确题目数。
3.运算答案不能小于0,小于0就重新在后台出题目。
三.个人开发流程:
|
PSP阶段 |
预计花费时间(h) |
实际花费时间(h) |
|
计划(Planning) |
2 |
3 |
|
开发(Development) |
48 |
60 |
|
分析需求(Analysis) |
4 |
6 |
|
生成设计文档(Design Spec) |
4 |
6 |
|
设计复审(Design Review) |
2 |
4 |
|
规范代码(Coding Standard) |
2 |
3 |
|
具体设计(Design ) |
4 |
7 |
|
具体编码(Coding) |
8 |
12 |
|
代码复审(Code Review) |
4 |
7 |
|
测试(Test) |
2 |
3 |
|
测试报告(Test Report) |
2 |
2 |
|
事后总结(Postmortem) |
2 |
3 |
|
提出过程改进计划(Process Improvement Plan) |
2 |
4 |
四,代码实现:
1)主要运算
Public void operation(int n){
int op1,op2;
for (int i = 0; i < n; i++) {
num1 = new Random().nextInt(100)+1;
num2 = new Random().nextInt(100)+1;
num3 = new Random().nextInt(100)+1;
op1=new Random().nextInt(4)+1;
op2=new Random().nextInt(4)+1;
if(op1 == 1){
String str = num1 + "+" + num2;
if(op2 == 1){
result = add(num1,num2);
result = add(result,num3);
str = str + "+" + num3;/*++*/
}
else if(op2 == 2){
result = add(num1,num2);
while(result < num3){
num3 = new Random().nextInt(100)+1;
}
result = sub(result,num3);
str = str + "-" + num3;/*+-*/
}
else{
str = pri(op1,op2);
}
print(str);
}
else if(op1 == 2){
String str = num1 + "-" + num2;
String str = "";
if(op2 == 1){
result = sub(num1,num2);
while(Math.abs(result) > Math.abs(num3)){
num3 = new Random().nextInt(100)+1;
}
result = add(result,num3);
str = num1 + "-" + num2 + "+" + num3;
}
else if(op2 == 2){
while(num1 < num2){
num1=newRandom().nextInt(100)+1;
}
result = sub(num1,num2);
str = num1 + "-" + num2;
while(result < num3){
num3 = new Random().nextInt(100)+1;
}
result = sub(result,num3);
}
else{
str = pri(op1,op2);
}
print(str);
}
else if(op1 == 3){
String str = num1 + "*" + num2+"*" + num3;
result = mul(num1,num2);
result = mul(result,num3);
str = operation2(op1,op2,str);
print(str);
}
else if(op1 == 4){
String str = num1 + "÷" + num2;
result = div(num1,num2);
str = operation2(op1,op2,str);
print(str);
}
else {
System.out.println("出错了。。。。");
}
}
System.out.println(n+"题中"+"您一共答对了"+mark+"题。");
}
2)对先进行加(减)法再进行乘(除)法的算式进行优先级判断和处理
public String pri(int op1,int op2){
String str = "";
if(op2 == 3){
result = mul(num2,num3);
str = comPri(op1,op2,str);
str = str + "*" + num3;
}
if(op2 == 4){
result = div(num2,num3);
str = comPri(op1,op2,str);
str = str + "÷" + num3;
}
return str;
}
public String comPri(int op1,int op2,String str){
if(op1 == 1){
result = add(num1,result);
str = num1 + "+" + num2;
}
if(op1 == 2){
while(num1 < result){
num2 = new Random().nextInt(100)+1;
num3 = new Random().nextInt(100)+1;
result = mul(num2,num3);
}
result = sub(num1,result);
str = num1 + "-" + num2;
}
return str;
}
3)对除法中的运算进行分析
public String operation2(int op1,int op2,String str){
if(op2 == 1){
result = add(result,num3);
return str + "+" + num3;
}
else if(op2 == 2){
while(result < num3){
num1 = new Random().nextInt(100)+1;
num2 = new Random().nextInt(100)+1;
num1,num2*/
result = div(num1,num2);
str = num1 + "÷" + num2;
}
result = sub(result,num3);
return str + "-" + num3;
}
else if(op2 == 3){
result = mul(result,num3);
return str + "*" + num3;
}
else{
result = div(result,num3);
return str + "÷" + num3;
}
}
4)输出代码
public void print(String str){
Scanner s = new Scanner(System.in);
System.out.print(str + "=");
int input = s.nextInt();
if(input == result){
mark=mark+1;
System.out.println("回答正确!");
System.out.println("已答对"+mark+"题。");
}
else {
System.out.println("回答错误!正确答案为:" + str + "=" +result);
}
}

浙公网安备 33010602011771号