Java算数运算测试

该博客介绍了一个使用Java编程实现的100以内加减法测验系统,该系统能生成数学题,接收用户答案并判断正误,最后给出分数排名,并将成绩保存到文件中。博主在实现过程中复习了面向对象编程、Java基础知识及数据库操作,并分享了学习心得。
摘要由CSDN通过智能技术生成


文章目录

  • 第一章 选题

    第二章 课题设计

    第三章 代码实现

     第四章 运算结果界面截图

    第五章 总结与心得


前言

选题:实现十道100以内加减法数学题,能根据题目计算出答案,与输入答案对比,判断做题是否正确,最后计算分数。每道题10分,答对得10分,答错不得分。最后计算成绩为多少分。


第二章 课题设计

面向对象编程思维和软件工程思想。

使用Java知识

Java基本输入输入出、运算符、循环、选择分支; JDBC,数据库,mysql。

功能模块图:

 

三代码实现

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;
import java.util.Scanner;

import static java.lang.System.*;

public class suanshu {


    public static void main(String[] args){
        int i,j;
        out.println("算数测试开始!");
        Scanner sc=new Scanner(in);
        out.println("请输入参与的人数:");
        int personNumber=sc.nextInt();

        int[] a1=new int[personNumber];   //存分数
        long[] b1=new long[personNumber];  //存时间
        Random ran=new Random();
        Scanner input = new Scanner(System.in);
        for(i=1;i<=a1.length;i++)//外层循环控制答题人数,内层循环控制题的数量
        {
            int count=0;            //记录每个人的答对次数
            long starttime= currentTimeMillis();//开始时间
            out.println("算术运算开始!");
            for(j=1;j<=10;j++)
            {
                int a=ran.nextInt(50);
                int b=ran.nextInt(50);
                int c=ran.nextInt(2);        //c==0执行加法,c==1执行减法


               // Random input = new Random();

                if(c==0)
                {
                    out.println(a+"+"+b+"=?");
                    int answer=input.nextInt();
                    if(a+b==answer)
                    {
                        count++;
                    }else{
                        continue;
                    }
                }else if(c==1){
                    out.println(a+"-"+b+"=?");
                    int answer;
                    answer = input.nextInt();
                    if(a+b==answer)
                    {
                        count++;
                    }else{
                        continue;
                    }
                }
            }
            long endtime= currentTimeMillis();        //结束时间
            long time=(endtime-starttime)/1000;
            a1[i-1]=10*count;
            b1[i-1]=time;
            out.println("第"+i+"位同学的答题情况:");
            out.println("答对"+count+"道题,得分为:"+(10*count)+",用时"+time+"秒");
        }
        for(int x=0;x<a1.length-1;x++)        //成绩排行榜
        {
            for(int y=0;y<a1.length-1-x;y++)
            {
                if(a1[y]<a1[y+1])
                {
                    int temp=a1[y];
                    a1[y]=a1[y+1];
                    a1[y+1]=temp;
                    long t=b1[y];
                    b1[y]=b1[y+1];
                    b1[y+1]=t;
                }
            }
        }
        for(int x=0;x<a1.length-1;x++)
        {
            for(int y=0;y<a1.length-1-x;y++)
            {
                if(a1[x]<a1[y])
                {
                    if(b1[x]>b1[y])
                    {
                        long temp=b1[x];
                        b1[x]=b1[y];
                        b1[y]=temp;
                    }
                }
            }
        }
        out.println();
        out.println("************成绩排行榜************");
        out.println("名次\t答对题数\t总分\t所用时间(s)");
        for(int z=0;z<a1.length;z++)
        {
            out.println(" "+(z+1)+"\t  "+(a1[z]/10)+"\t  "+a1[z]+"\t  "+b1[z]);
        }
        //把排行榜存放到文件(c:\\算术成绩排行榜.txt)
        Writer s=null;
        BufferedWriter f=null;
        try{
            s=new FileWriter("c:\\算术成绩排行榜.txt");
            f=new BufferedWriter(s);
            f.write("名次\t答对题数\t总分\t所用时间(s)");
            f.newLine();
            for(int z=0;z<a1.length;z++)
            {
                f.write(" "+(z+1)+"\t  "+(a1[z]/10)+"\t  \t"+a1[z]+"\t\t"+b1[z]);
                f.newLine();
            }
            out.println("已保存");
            f.flush();
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                f.close();
                s.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
}

预算结果截图:
 

 

总结

心得:回顾知识点的同时也增加了新知识数据库以及mysql和GUI的学习阶段1较短时间使我对数据库这方面感到自身水平的不总,但是我也在不断地学习和改进,与同学和老师的交流使我受益匪浅,我会慢慢提升自己,,即使学习路上会遇到很多困难和挫折,但也不会阻止我学习的道路

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个Java小学算术运算测试程序的示例: ```java import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; public class ArithmeticTestProgram extends Frame implements ActionListener { private Label label1, label2, label3, label4, label5, label6, label7, label8, label9, label10; private TextField textField; private Button button; private ArrayList<String> questions; private ArrayList<String> answers; public ArithmeticTestProgram() { setTitle("小学生四则运算测试系统-郑州轻工业大学-***-开发,****..");//设置标题 setLayout(new GridLayout(12, 1)); label1 = new Label("小学生四则运算测试系统"); label2 = new Label("请输入题目数量:"); textField = new TextField(); button = new Button("开始测试"); button.addActionListener(this); add(label1); add(label2); add(textField); add(button); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { int num = Integer.parseInt(textField.getText()); generateQuestions(num); saveQuestionsToFile(); } } private void generateQuestions(int num) { questions = new ArrayList<>(); answers = new ArrayList<>(); for (int i = 0; i < num; i++) { int operand1 = (int) (Math.random() * 100); int operand2 = (int) (Math.random() * 100); int operator = (int) (Math.random() * 4); String question = ""; String answer = ""; switch (operator) { case 0: question = operand1 + " + " + operand2 + " = "; answer = String.valueOf(operand1 + operand2); break; case 1: question = operand1 + " - " + operand2 + " = "; answer = String.valueOf(operand1 - operand2); break; case 2: question = operand1 + " * " + operand2 + " = "; answer = String.valueOf(operand1 * operand2); break; case 3: question = operand1 + " / " + operand2 + " = "; answer = String.valueOf(operand1 / operand2); break; } questions.add(question); answers.add(answer); } } private void saveQuestionsToFile() { File file = new File("questions.txt"); try { FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); for (int i = 0; i < questions.size(); i++) { String question = questions.get(i); String answer = answers.get(i); String line = question + answer + "\n"; bos.write(line.getBytes()); } bos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ArithmeticTestProgram program = new ArithmeticTestProgram(); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值