小学数学练习

本文介绍了一个设计用于小学生数学练习的程序,该程序要求用户输入ID,提供四种运算的选择,生成10道100以内的随机运算题目,并记录答题时间和成绩。程序通过随机数生成算法确保题目涵盖所有运算类型,并将成绩和用时保存到文件中。
摘要由CSDN通过智能技术生成

小学数学练习

设计题目
编写一个帮助小学生练习数学的程序,帮助小学生练习 100 以内的四种数学运算:加、减、乘、除。

问题描述
a) 程序应先询问用户的 ID 号(ID 号包括两个大写字母和 4 位数字),例如:
请输入用户 ID 号:AB1234
程序应对输入的 ID 号验证,符合 ID 号要求的格式,然后程序提示三种选择:
(1)开始测试 (2)检查分数 (3)退出
b) 测试:该程序将给出 10 道数学题,例如: 12 * 3 =36 48 + 32 =80 … 56 / 28 =2
注意: i)学生将依次回答每一个问题(在等于号后面给出答案),然后给出下一道题。
ii)试题应包含四种数学运算:加、减、乘、除,它们是随机产生的。相邻的问题应该是不同的操作, 每个操作必须至少出现一次。报告中应给出实现方法或算法。
iii)为每道题随机生成数字,但必须确保参与运算的数字和结果都小于 100 且大于零的整数,除法时 还要注意两个整数要能整除。报告中应给出实现方法或算法。
iv)十道题做完后,记录学生完成这十道题所用的时间。
v)给每个学生一个分数。将该学生的 ID、成绩和使用时间保存到一个名为 record.txt 的文件中。
vi)在屏幕上输出以下信息:(3 列信息,第 1 列是 b)中的测试题,蓝色部分)
问题 | 正确答案 | 你的答案
c) 成绩检查:从文件“record.txt”中列出该学生的所有历史成绩(其他学生的不显示)。
例如: 你以前的记录是:
AB1234 80 150 秒
AB1234 50 182 秒
AB1234 90 98 秒

问题分析
对比ID是否匹配,匹配ID后给出选项,测试内容:10个计算题,加减乘除的数为100以内,相邻两个式子类型不同,且10个题中四类运算均要出现,计算用户答题时间,保留每个题用户答案及其正确答案,最后输出,将ID及得分用时存入文件,用户调用时输出。

**功能实现
对比ID是否匹配:正则表达式
匹配ID后给出选项:switch语句
加减乘除的数为100以内,相邻两个式子类型不同:随机数确定计算类型,设置条件使得符合要求
计算用户答题时间:currentTimeMillis()语句
保留每个题用户答案及其正确答案:出题时将其存储最后使用循环输出
将ID及得分用时存入文件,用户调用时输出:文件的使用
**

详细代码

package com.company;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.util.regex.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Id.id();
    }
}
class Id{
    public static void id() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户ID号:");
        String id = sc.next();
        String regex="\\p{Upper}{2}\\d\\d\\d\\d";
        boolean result = id.matches(regex);
        if(result){
            System.out.println("(1)开始检测");
            System.out.println("(2)检查分数");
            System.out.println("(3)退出");
            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    long start = System.currentTimeMillis();
                    Question.sum();
                    Question.subtraction();
                    Question.multiplication();
                    Question.division();
                    int z =(int)(Math.random()*10)%4+1;
                    for(int i = 0;i<6;i++){
                        if(z==1){
                            do{
                                z=(int)(Math.random()*10)%4+1;
                            }while (z==1);
                            Question.sum();
                        }
                        else if(z==2) {
                            do{
                                z=(int)(Math.random()*10)%4+1;
                            }while (z==2);
                            Question.subtraction();
                        }
                        else if(z==3) {
                            do{
                                z=(int)(Math.random()*10)%4+1;
                            }while (z==3);
                            Question.multiplication();
                        }
                        else{
                            do{
                                z=(int)(Math.random()*10)%4+1;
                            }while (z==4);
                            Question.division();
                        }
                    }
                    long end = System.currentTimeMillis();
                    long time = (end-start)/1000;
                    Record.writeFile(id,Question.score,time);
                    System.out.println("问题 | 正确答案 | 你的答案");
                    Answers.getQuestionAndAnswersAndInputs();
                    System.out.println("得分为:"+Question.score);
                    System.out.println("历时:"+time+"秒");
                    String data =id + "    " + Question.score + "    " + time;//将ID,分数,用时转化成字符串便于储存。
                    break;
                case 2:
                    Record.readTxt(id);
                    break;
                case 3:
                    System.out.println("下次见啦");
                    break;
                default:
                    System.out.println("该选项不存在!");
            }
        }else{
            System.out.println("用户名不合法");
        }
    }
}
class Question{
    static int score = 0;
    public static void sum(){
        Scanner ss = new Scanner(System.in);
        int x =(int)(Math.random()*99+1);
        int y =(int)(Math.random()*(99-x)+1);
        int m =x+y;
        System.out.print(x+"+"+y+"=");
        int a = ss.nextInt();
        if(a==x+y){
            score+=10;
        }
        Answers.questions.add(x+"+"+y+"=");
        Answers.answers.add(m);
        Answers.inputs.add(a);
    }
    public static void subtraction(){
        Scanner ss = new Scanner(System.in);
        int x =(int) (Math.random()*99+1);
        int y =(int)(Math.random()*100);
        int m = x-y;
        if(x>y){
            System.out.print(x+"-"+y+"=");
        }else{
            int temp = y;
            y = x;
            x = temp;
            System.out.print(x+"-"+y+"=");
        }
        int a = ss.nextInt();
        if (a == x - y) {
            score += 10;
        }
        Answers.questions.add(x+"-"+y+"=");
        Answers.answers.add(m);
        Answers.inputs.add(a);
    }
    public static void multiplication(){
        Scanner ss = new Scanner(System.in);
        int x ;
        int y ;
        int m ;
        do{
            x =(int)(Math.random()*99+1);
            y =(int)(Math.random()*99+1);
            m=x*y;
        }while(x*y>100);
        System.out.print(x + "*" + y + "=");
        int a = ss.nextInt();
        if (a == x * y) {
            score += 10;
        }
        Answers.questions.add(x+"*"+y+"=");
        Answers.answers.add(m);
        Answers.inputs.add(a);
    }
    public static void division(){
        Scanner ss = new Scanner(System.in);
        int x =(int) (Math.random()*99+1);
        int y =((int)(Math.random()*(100/x)+1))*x;
        int m = y/x;
        System.out.print(y + "/" + x + "=");
        int a = ss.nextInt();
        if (a == y / x) {
            score += 10;
        }
        Answers.questions.add(y+"/"+x+"=");
        Answers.answers.add(m);
        Answers.inputs.add(a);
    }
}
class Record{
    public static void writeFile(String ID , int score , long usedTime) {
        try {
            File file = new File("filestream.txt");
            if(!file.exists()) {
                file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
            }
            FileOutputStream fos = new FileOutputStream(file,true);//输出流
            OutputStreamWriter osw = new OutputStreamWriter(fos);//转换流
            BufferedWriter bw = new BufferedWriter(osw);//缓冲输入流
            bw.write(ID + " " + score + " " + usedTime + " 秒");
            bw.newLine();
            bw.flush();
            bw.close();
            osw.close();
            fos.close();
        }catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    public static String readTxt(String ID) {
        File file = new File("filestream.txt");
        if(file.isFile() && file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String text;
                while((text = bufferedReader.readLine()) != null){
                    if(text.contains(ID))
                        System.out.println(text);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
class Answers{
    static ArrayList<String>questions=new ArrayList<>();
    static ArrayList<Integer>answers=new ArrayList<>();
    static ArrayList<Integer>inputs=new ArrayList<>();
    public static void getQuestionAndAnswersAndInputs(){
        for(int i=0;i<10;i++){
            System.out.println(questions.get(i)+"    "+answers.get(i)+"     "+inputs.get(i));
        }
    }
}

对于随机数的定义还有很多方式
如放入do while 循环语句加法直至和为100,减法差大于0,乘法积小于100,除法余数为0。
对于如何确保四种计算种类都出现,这里使了个小聪明即定死前四题为加减乘除,其余六题进行循环。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值