计应193王思梦 小学生四则运算出题系统

一.计划

 编写一个小学生四则运算出题程序

二.开发

1.需求分析

作为一名一年级小学生的家长,我希望制作一个出题软件,随机产生100以内的正整数加减乘除及符号,减法为负值的剔除,剔除不够减,循环产生。

2.生成设计文档

3.设计复审

4.代码规范

注意大小写、缩进、括号及分行。

5.具体设计

系统开始,随机产生10道加法、减法、乘法或除法,计算完成后,显示答案及对错,统计时间,系统结束。

6.具体编码

package Math;
public class MathQuestions {
private int num1;
private int num2;
private String q;
private static int grade = 0;
public MathQuestions() {
}
public MathQuestions(int num1,int num2,String q) {
this.num1 = num1;
this.num2 = num2;
this.q= q;
}
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public String toString() {
return this.num1 + this.q+ this.num2;
}
public int getResult() {
int result = 0;
switch(this.operateEle) {
case "+": result = this.num1 + this.num2;break;
case "-": result = this.num1 - this.num2;break;
case "*": result = this.num1 * this.num2;break;
case "/": result = this.num1 / this.num2;break;
}
return result;
}
public static void getGrade() {
System.out.println( "考试结束,最后成绩为:" + MathQuestions.grade );
}
public static void setGrade(int grade) {
MathQuestions.grade += grade;
}
public static void clear() {
MathQuestions.grade = 0;
}
}

package Math;
import java.util.Random;
import java.util.Scanner;
public class MathQuestions1 {
public static int getRandom() {
Random ran = new Random();
return ran.nextInt(100);
}
public static String getq() {
String[] q = {"+","-","*","/"};
Random ran = new Random();
return q[ran.nextInt(4)];
}
public static int getInput() {
Scanner scan = new Scanner(System.in);
int result = 0;
System.out.println( "请输入结果:" );
result = scan.nextInt();
return result;
}
}

package Math;
import Math.MathQuestions1;
import Math.MathQuestions;
public class MathQuestions2 {
public static void main(String args[]) {
int i = 0;
int inputResult = 0;
do {
MathQuestions ts = new MathQuestions(MathQuestions1.getRandom(),MathQuestions1.getRandom(),MathQuestions1.getq());
System.out.println(ts);
inputResult = MathQuestions1.getInput();
if( inputResult == ts.getResult() ) {

System.out.println( "回答正确!" );
MathQuestions.setGrade(20);
}else {

System.out.println( "回答错误!" );
}
i++;

}while(i < 5);

MathQuestions.getGrade();
MathQuestions.clear();

}
}

 

 

posted @ 2021-05-31 12:03  草莓曲奇饼  阅读(76)  评论(0编辑  收藏  举报