zoukankan      html  css  js  c++  java
  • 四则运算小试牛刀

    本次作业来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2166

    我的GitHub地址:https://github.com/570048926/Software

    第一部分:个人软件过程耗时估计与统计表

    1.PSP时间规划

    PSP2.1 Personal Software Process Stages Time Senior Student Time
    Planning 计划 12 8
    · Estimate 估计这个任务需要多少时间 10 6
    Development 开发 82 88
    · Analysis 需求分析 (包括学习新技术) 6 10
    · Design Spec 生成设计文档 5 6
    · Design Review 设计复审 4 6
    · Coding Standard 代码规范 4 4
    · Design 具体设计 10 12
    · Coding 具体编码 36 21
    · Code Review 代码复审 7 9
    · Test 测试(自我测试,修改代码,提交修改) 15 30
    Reporting 报告    
    · 测试报告    
    · 计算工作量 3 2
    · 并提出过程改进计划 4 3

           对于第一次用PSP来规划做事情,我的感想觉得有点不习惯,因为之前没有好好地规划过这些事。可能因为没有适应这样的做法,按部就班的去做比之前速度慢了一点。但是有了PSP时间规划表可以明确的知道自己一步一步的执行计划,在实践过程中减少了时间的浪费,也减少了打代码期间发生了错误。

    第二部分:程序运行

    1、随机生成题目(截图)

            通过测试可以生成两个运算符的三个运算数的四则运算,按照输入的题目数自动生成所需题目。并且可以按照运算法则来计算绕后输入答案即可。

    2.程序源码

    package SZYS;
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class Calculate {
        public static void main(String[] args) {
            int totalCnt = 0; //总题数
            int cnt = 0;      //当前已完成的题数,用来做循环的判断
            int rightCnt = 0; //做正确的题数
            int errorCnt = 0;  //做错误的题数
            int num;
            System.out.print("请输入你要做的题数:");
            Scanner input = new Scanner(System.in);
            totalCnt = input.nextInt();
            while (cnt < totalCnt) {
                int x=(int)(Math.random()*100);
                int y=(int)(Math.random()*100);
                int z=(int)(Math.random()*100);
                String result;
    
                switch ((int)(Math.random()*4))
                {
                    case 0: //+
                       switch ((int)(Math.random()*4)){
                            case 0: //+
                                System.out.println(x+"+"+y+"+"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x+y+z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x+y+z) );
                                }
                                break;
                            case 1: //-
                                System.out.println(x+"+"+y+"-"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x+y-z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x+y-z) );
                                }
                                break;
                            case 2: //*
                                System.out.println(x+"+"+y+"*"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x+y*z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x+y*z) );
                                }
                                break;
                            case 3: // /
                                System.out.println(x+"+"+y+"/"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x+y/z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x+y/z) );
                                }
                                break;
                        }
    
                        break;
                    case 1:
                        if(x<y)
                        {
                            int temp=x;
                            x=y;
                            y=temp;
                        }
                        switch ((int)(Math.random()*4)){
                            case 0: //+
                                System.out.println(x+"-"+y+"+"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x-y+z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x-y+z) );
                                }
                                break;
                            case 1://-
                                int k = x-y;
                               if (k<z)
                                {
                                    int temp=(x-y);
                                    k=z;
                                    z=temp;
                                }
                                System.out.println(x+"-"+y+"-"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x-y)-z )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x-y-z) );
                                }
                                break;
                            case 2://*
                                System.out.println(x+"-"+y+"*"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x-y*z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x-y*z) );
                                }
                                break;
                            case 3:// /
                                System.out.println(x+"-"+y+"/"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x-y/z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x-y/z) );
                                }
                                break;
                        }
                        break;
                    case 2:
                        switch ((int)(Math.random()*4))
                        {
                            case 0:
                                System.out.println(x+"*"+y+"+"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x*y+z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x*y+z) );
                                }
                                break;
                            case 1:
                                System.out.println(x+"*"+y+"-"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x*y-z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x*y-z) );
                                }
                                break;
                            case 2:
                                System.out.println(x+"*"+y+"*"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x*y*z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x*y*z) );
                                }
                                break;
                            case 3:
                                System.out.println(x+"*"+y+"/"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x*y/z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x*y/z) );
                                }
                                break;
                        }
                        break;
                    case 3:
                        switch ((int)(Math.random()*4))
                        {
                            case 0:
                                while(y==0)
                                    y= (int)(Math.random()*100);
                                System.out.println(x+"/"+y+"+"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x/y+z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x/y+z) );
                                }
                                break;
                            case 1:
                                while(y==0)
                                    y= (int)(Math.random()*100);
                                System.out.println(x+"/"+y+"-"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x/y-z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x/y-z) );
                                }
                                break;
                            case 2:
                                while(y==0)
                                    y= (int)(Math.random()*100);
                                System.out.println(x+"/"+y+"*"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x/y*z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x/y*z) );
                                }
                                break;
                            case 3:
                                while(y==0)
                                {
                                    y= (int)(Math.random()*100);
                                }
                                while (z==0)
                                {
                                    z = (int)(Math.random()*100);
                                }
                                System.out.println(x+"/"+y+"/"+z+"=");
                                result = input.nextLine();
                                while(result.length() == 0 ||result.equals('\n') || result.equals('\r')){
                                    result = input.nextLine();
                                }
                                num=Integer.parseInt(result);
                                if (num == (x/y/z) )
                                {
                                    rightCnt ++;
                                    System.out.println("答案正确!" );
                                }
                                else
                                {
                                    errorCnt ++;
                                    System.out.println("答案错误!正确答案为:"+(x/y/z) );
                                }
                                break;
                        }
                        break;
                }
                cnt++;
            }
            System.out.println("共"+totalCnt+"题,答对:"+rightCnt+"题,答错:"+errorCnt+"题!" );
            System.out.print("你的正确率为:"+((float)rightCnt/(float)totalCnt)*100+"%,错误率为:"+((float)errorCnt/(float)totalCnt)*100+"%");
    
        }
    }

    3、把项目存在GitHub库上

    第三部分:分析与总结

    1、本次程序实现的功能

    (1)这次四则运算的程序实现了四则运算,自动生成用户所输入题数的三个100以内的运算数和两个运算符的四则运算,每生成一题便要输入答案才可以输出下一题然后在输入答案,以此类推。

    (2)可以根据用户输入判断对错,统计错误和正确的题目数,最后按照百分比算出所做题目的正确率和错误率。

    2、程序的缺点与不足

    (1)在三个运算数的某些情况下会出现得数为零的现象(自己在解决中)。

    (2)用户不能输入真分数的格式来回答答案。

    (3)在除法,只做了得到答案的整数部分。如45/65 = 0, 99/32 = 3

    (4)本次作业过程中,比起计划的时间,实际上还用得更多的时间来构想和实现;使用java语言编写代码不够熟练。

    3、后期改进

          对于本次作业未实现的功能,紧接着要按要求做好。实现整个程序的所需要的功能要求,以及对java语言的熟练程度进行提升,目前已通过网络上的视频学习来巩固一下专业的知识了。

    4、本次作业感想

            因为之前了解过做四则运算的小程序,不过之前是用C语言实现的,这次看到了这个程序本能觉得自己可以用比较不熟练java尝试一下,但是效果并没有自身预想的一样轻易就实现了。对于时间规划方面,我仍需要更进一步的做好,要把时间合理分配养成一种习惯。

  • 相关阅读:
    在mvc中,使用a链接,怎么转到别的html中
    mvc中怎么读取值传到后台中方法之一(表单传值法)
    mvc中怎么带参传递
    sqlserver去掉字符串结尾的全角空格并用半角替换
    Ajax学习笔记
    Ajax级联实例
    [转]js导航栏处于选中状态
    asp.net GridView的使用
    keycode大全
    IsPostBack的使用
  • 原文地址:https://www.cnblogs.com/ShaoJingWen/p/9764975.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com