zoukankan      html  css  js  c++  java
  • 结对编程1-基于GUI的四则运算生成器

    https://git.coding.net/kjc976118457/work2.git

    201421123017 康建灿 201421123016 郑怀勇

    一、需求分析

    1. 记录用户的对错总数。
    2. 程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算。
    3. 有计时功能,能显示用户开始答题后的消耗时间。
    4. 具有GUI界面

    二、功能设计

    三、代码展示:

     文件数据保存:

    public class pro{
        int t=0,f=0;
                     void file(int d,int count) {
                         File file = new File("D:/text.txt");
                         String line =null;
                        try {
                            BufferedReader br = new BufferedReader(new FileReader(file));
                             try {
                                 while((line = br.readLine()) != null){
                                     String[] num = line.split("#");
                                     t=Integer.parseInt(num[1]);
                                     f=Integer.parseInt(num[3]);
                                 }} catch (IOException ex) {
                             }
                        } catch (FileNotFoundException ex) {
                            get(d,count);
                        }
                         get(d,count);
                     }
                            public void get(int d,int count) {
                                  FileOutputStream fop = null;
                                  File file;
                                  String content = "    #"+(d+t)+"# ȷ#"+(count+f)+"#";
                                  try {
                                   file = new File("D:/text.txt");
                                   fop = new FileOutputStream(file);
                                   if (!file.exists()) {
                                    file.createNewFile();
                                   }
                                   byte[] contentInBytes = content.getBytes();
                                   fop.write(contentInBytes);
                                   fop.flush();
                                   fop.close();
                                  } catch (IOException e) {
                                   e.printStackTrace();
                                  } finally {
                                   try {
                                    if (fop != null) {
                                     fop.close();
                                    }
                                   } catch (IOException e) {
                                    e.printStackTrace();
                                   }
                                  }
                            }
    
      
                     }
    

      GUI主界面:

    public class start extends JFrame implements ActionListener{
    
        private JLabel numText;
        private JLabel tagLabel,messageLabel;
        private JTextField numEdit;
        private JButton commitBtn;
        
        public start(){
            this.setBounds(500, 200, 300, 300);
            this.setResizable(false);
            this.setTitle("        ");
            
            initView();
        }
        
        private void initView(){
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout(5,5));
            
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(2,2));
            numEdit = new JTextField();
            commitBtn= new JButton("ȷ ");
            commitBtn.setFont(new java.awt.Font("   ", 100, 32));
            commitBtn.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            commitBtn.addActionListener(this);
            
            panel.add(new JLabel("     ɼ     Ŀ "));
            panel.add(numEdit);
            numEdit.setFont(new java.awt.Font("    ", 100, 32));
            numEdit.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            
            JPanel panel2 = new JPanel();
            panel2.setLayout(new GridLayout(2,1));
            
            tagLabel = new JLabel();
            
            panel2.add(tagLabel);
            panel2.add(commitBtn)
            ;
            mainPanel.add("Center",panel);
            mainPanel.add("South",panel2);
            
            add(mainPanel);
        }
    
        @Override
        public void actionPerformed(ActionEvent arg0) {
            String num = numEdit.getText();
            boolean pass = false;
            if(num==null||num.equals("")){
                messageLabel.setText("        Ŀ     ");
                return;
            }
            else pass=true;
            if(pass){
                int m=Integer.parseInt(num);
                new test(m).show();
                this.dispose();
            }else{
                messageLabel.setText("          ");
            }
            
        }
    }

    计时器:

        timer.start();
            timeText.setText("  ʱ  :"+timeText.getText());
            //timeText.setText("time");
            commitBtn = new JButton("ȷ  ");
            commitBtn.addActionListener(this);
            
            commitBtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                    for(int i=0;i<m;i++){
                        String answer = answerEdit[i].getText();
                           if(answer.equals(c[i])){
                              count++;
                           }
                    }
                    int d=(int)m-count;
                    pro r=new pro();
                    r.file(d,count);
                    String time= timeText.getText();
                       double g=(double)count/m;
                       new result(m,count,g,time).show();
                   }
    
            });
            

    生成四则运算:

       private void check(String answer) {
            
        }
    
        public static String  Fenshu(int i,String b[],int a[][],String c[]){
            int x=(int)(0+Math.random()*(9-0+1));
            int y=(int)(0+Math.random()*(9-0+1));
            while(x>=y||y==0||x==0)
            {
                 x=(int)(0+Math.random()*(9-0+1));
                 y=(int)(0+Math.random()*(9-0+1));
            }
            int k=(int)(0+Math.random()*(9-0+1));
            int t=(int)(0+Math.random()*(9-0+1));
            while(k>=t||t==0||k==0)
            {
                k=(int)(0+Math.random()*(9-0+1));
                t=(int)(0+Math.random()*(9-0+1));
            }
            int o=(int)(0+Math.random()*(3-0+1));
            if(o==0)
            {
                b[i]="("+x+"/"+y+")+("+k+"/"+t+")=";
                a[i][0]=x*t+k*y;
                a[i][1]=y*t;
                int n=divisor(a[i][0],a[i][1]);
                c[i]=a[i][0]/n+"/"+a[i][1]/n;
            }
            if(o==1)
            {
                b[i]="("+x+"/"+y+")-("+k+"/"+t+")=";
                a[i][0]=x*t-k*y;
                a[i][1]=y*t;
                int n=divisor(a[i][0],a[i][1]);
                c[i]=a[i][0]/n+"/"+a[i][1]/n;
            }
            if(o==2)
            {
                b[i]="("+x+"/"+y+")*("+k+"/"+t+")=";
                a[i][0]=x*k;
                a[i][1]=y*t;
                int n=divisor(a[i][0],a[i][1]);
                c[i]=a[i][0]/n+"/"+a[i][1]/n;
            }
            if(o==3)
            {
                b[i]="("+x+"/"+y+")  ("+k+"/"+t+")=";
                a[i][0]=x*t;
                a[i][1]=k*y;
                int n=divisor(a[i][0],a[i][1]);
                c[i]=a[i][0]/n+"/"+a[i][1]/n;
            }
            return b[i]+c[i];
     };
     public static String Zhengshu(int i,String b[],String c[]){
             int x=(int)(0+Math.random()*(100-0+1));
             int y=(int)(0+Math.random()*(100-0+1));
             int z=(int)(0+Math.random()*(3-0+1));
             if(z==0){
                b[i]=x+"+"+y+"=";
                c[i]=x+y+"";
             }
             if(z==1){
             b[i]=x+"-"+y+"=";
             c[i]=x-y+"";
             }
             if(z==2){
             b[i]=x+"*"+y+"=";
             c[i]=x*y+"";
             }
             if(z==3){
             b[i]=x+"  "+y+"=";
             int n=divisor(x,y);
             c[i]=x/n+"/"+y/n;
             }
            return b[i]+c[i];
         }
     public static int divisor(int i, int j)
    {
     int k=1,s;
        for(s=1;s<=i&&s<=j;s++){
        if(i%s==0&&j%s==0)
            k=s;
        }
        return k;
    
    }

    四、测试运行截图:

    五、PSP

    PSP2.1 Personal Software Process Stages Estimated time(h) actual time(h)
    Planning 计划 1 1.5
    · Estimate 估计这个任务需要多少时间 24 40
    Development 开发 24 34
    · Analysis 需求分析 (包括学习新技术) 1 1.2
    · Design Spec 生成设计文档 7 10
    · Design Review 设计复审 0.5 0.5
    · Coding Standard 代码规范 0.5 0.8
    · Design 具体设计 1.5 1.5
    · Coding 具体编码 20 30
    · Code Review 代码复审 10 15
    · Test 测试(自我测试,修改代码,提交修改) 1 3
    Reporting 报告 20 25
    · 测试报告 1 1.5
    · 计算工作量 0.5 1
    · 并提出过程改进计划 2 4

    六、总结

    经过将近一星期的对题目的理解与分析,虽然最终完成了任务,但在这过程中发现了许多问题。

    首先,因为Java是在大二的时候学习的,经过一年的时间,加上平时编程量太少,对GUI界面的编程基本上忘光了,许多步骤了程序需要重新再学习。

    其次,第一次作业的程序在这次发现有许多不完善的地方需要改进。花费了不少时间。

    再次,关于两人的分工探讨,对题目的理解与解决方法有许多不同的见解需要达成一致。特别是文件存储和图形界面,更是花费了好一阵子。

    最后,对两人在此次的完成作业的过程做出所谓“汉堡包”式的评价:

    先说贬义的:很明显两个人都是懒人协会的,对作业只会一拖再拖,不到万不得已绝不动程序。

    来在夸自己的:在时间紧迫的关键时候我们总能爆发出背水一战的勇气,完成任务的速度飞快。

    最后在批判一下:或许程序员真的不适合我们,两个人的编程技术真的是烂到家里。

    七,综上,附上合照一张

  • 相关阅读:
    MySQL5.7修改字符集
    MySQL-day1数据库的安装与介绍
    简述Python中的break和continue的区别
    Python实现用户交互,显示省市县三级联动的选择
    Mac升级Node.js和npm到最新版本指令
    vue+Typescript初级入门
    js-md5加密
    create-react-app 工程,如何修改react端口号?
    chrome安装react-devtools开发工具插件
    mac下更新node版本
  • 原文地址:https://www.cnblogs.com/kkjc/p/6547995.html
Copyright © 2011-2022 走看看