小学数学练习
1、问题描述
编写一个帮助小学生练习数学的程序,帮助小学生练习 100 以内的四种数学运算:加、减、乘、除。
2、要求
(1)程序应先询问用户的 ID 号(ID 号包括两个大写字母和 4 位数字),例如: 请输入用户 ID 号:AB1234,ID无误后方可进入菜单界面。
(2)对菜单的要求:必须有的功能有测试、检查历史成绩、检查答题情况、退出系统
(3)对题目的要求:学生将依次回答每一个问题(在等于号后面给出答案),然后给出下一道题。试题应包含四种数学运算:加、减、乘、除,它们是随机产生的。相邻的问题应该是不同的操作, 每个操作必须至少出现一次。每道题随机生成数字,必须确保参与运算的数字和结果都小于 100 且大于零的整数,除法时 还要注意两个整数要能整除。十道题做完后,记录学生完成这十道题所用的时间。
(4)对输出格式的要求:历史成绩包含id、分数、答题所用时间,例如AB1234 80 150 秒;检查答题情况包含问题、正确答案、使用者答案,格式如问题 | 正确答案 | 你的答案
3、对问题进行分析
首先分析对象,有学生和问题两个对象。再分析对象具有哪些属性,学生:id、分数、答题时间;问题:两个运算的数、运算符号、表达式、学生的答案、题目的正确答案。模块分析:在此我使用了四个类,学生类、问题类分别包含各自的字段、读写方法、覆写toString()方法;Dao类包含一些要使用的方法如判断id、生成10个相邻之间不相等的范围是0-3的数字、生成题目、学生参与测试、检查分数、打印历史成绩;Main类包含main()方法,实现菜单。
4、程序
首先学生类
public class Student {
private String id;
private int score;
private long time;
public Student() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
@Override
public String toString() {
return id+" "+score+" "+time+"秒";
}
}
问题类
public class Problem {
private int x;
private int y;
private int symbol;
private int result;
private int studentResult;
private String expression;
public Problem() {
}
public Problem(int x, int y, int symbol) {
this.x = x;
this.y = y;
this.symbol = symbol;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getSymbol() {
return symbol;
}
public void setSymbol(int symbol) {
this.symbol = symbol;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public int getStudentResult() {
return studentResult;
}
public void setStudentResult(int studentResult) {
this.studentResult = studentResult;
}
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
this.expression = expression;
}
public void matchSymbol(){
switch (symbol){
case 0:
result = x+y;
expression = x+"+"+y+"=";
break;
case 1:
result = x-y;
expression = x+"-"+y+"=";
break;
case 2:
result = x*y;
expression = x+"*"+y+"=";
break;
case 3:
result = x/y;
expression = x+"/"+y+"=";
break;
default:
break;
}
}
@Override
public String toString() {
return expression+"|"+result+"|"+studentResult;
}
}
Dao类进行数据处理,主要的功能实现就在这里了
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Dao {
public boolean judge(String id){
String str = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6}$";
return id.matches(str);
}
public int[] symbol(){
Random random = new Random();
int[] ints = new int[10];
int a;
for (int i = 0;i<10;i++){
a = random.nextInt(4);
ints[i] = a;
if (i>0&&ints[i]==ints[i-1]){
i--;
}
}
return ints;
}
public Problem[] createProblem(){
Random rd = new Random();
Problem[] problems = new Problem[10];
int[] symbol = symbol();
for (int i = 0;i<10;i++){
int x = rd.nextInt(99)+1;
int y = rd.nextInt(99)+1;
Problem temp = new Problem(x,y,symbol[i]);
temp.matchSymbol();
if (symbol[i]==3){
if (x%y==0){
problems[i] = temp;
}else {
i--;
}
}else {
if (temp.getResult()>0&&temp.getResult()<100){
problems[i] = temp;
}else {
i--;
}
}
}
return problems;
}
public void test(Student s) throws IOException {
long time1 = System.currentTimeMillis();
int correctNum = 0;
Scanner sc = new Scanner(System.in);
Problem[] problems = createProblem();
for (int i = 0;i<10;i++){
System.out.print(problems[i].getExpression());
problems[i].setStudentResult(sc.nextInt());
if (problems[i].getResult()==problems[i].getStudentResult()){
correctNum++;
}
}
long time2 = System.currentTimeMillis();
long time = (time2-time1)/1000;
s.setTime(time);
s.setScore(correctNum*10);
FileWriter writer = new FileWriter("D:\\record.txt",true);
BufferedWriter bw = new BufferedWriter(writer);
bw.write(s.toString());
bw.write("\n");
bw.flush();
FileWriter fileWriter = new FileWriter("D:\\problem.txt",true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("\n");
for (int i = 0;i<10;i++){
bufferedWriter.write(problems[i].toString());
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
public void check() throws IOException {
FileReader reader = new FileReader("D:\\problem.txt");
BufferedReader br = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer();
if (br.ready()){
String temp ;
while ((temp= br.readLine())!=null){
buffer.append(temp);
buffer.append("\n");
}
System.out.println(buffer.toString());
}else {
System.out.println("还未有过记录");
}
}
public void printAllData() throws IOException {
FileReader reader = new FileReader("D:\\record.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer();
if (bufferedReader.ready()){
String temp;
while ((temp=bufferedReader.readLine())!=null){
buffer.append(temp);
buffer.append("\n");
}
System.out.println(buffer.toString());
}else {
System.out.println("还未有记录");
}
}
}
最后进行测试
package Project11;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
Student student = new Student();
Dao dao = new Dao();
System.out.println("学生数学练习系统");
System.out.println("请输入你的id");
boolean flag = true;
boolean sign = true;
while (flag){
String id = sc.next();
if (dao.judge(id)){
student.setId(id);
while (sign){
System.out.println("请选择使用的功能:1、2、3、4");
System.out.println("(1)开始测试");
System.out.println("(2)检查分数");
System.out.println("(3)历史成绩");
System.out.println("(4)退出");
int input = sc.nextInt();
switch (input){
case 1:
dao.test(student);
System.out.println("学生成绩:"+student.getScore());
break;
case 2:
System.out.println("问题|答案|正确答案");
dao.check();
break;
case 3:
dao.printAllData();
break;
case 4:
sign = false;
flag = false;
break;
default:
System.out.println("请输入正确的功能序号");
break;
}
}
}else {
System.out.println("id有误");
flag = false;
}
}
}
}
|