zoukankan      html  css  js  c++  java
  • 【软件工程实践】结对项目四则运算 “软件”之升级版

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

    GitHub库地址:https://github.com/570048926/Software

    一、本次作业要求

    从个人项目出发,将程序改造成一个单机带用户界面(不是控制台)的程序,这个程序最基本要达到:

    • 自动生成题目,单个题目最多不能超过4个运算符,操作数小于100。
    • 用户可以输入答案
    • 若用户输入答案正确,则提示正确;若答案错误,则提示错误,并要提示正确答案是多少。

    二、本次扩展的方向

    1、程序可以出带括号的正整数四则运算,不支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67;

    2、用户答题结束以后,程序可以显示用户答题所用的时间;

    3、用户可以一次答题不能超过5个题目,若超过5个题目,答题结束可以显示用户答错的题目个数和答对的题目个数并统计分数;

    4、用户在答题时,需要用户输入用户名 ;

    5、程序可以设置答题时间,时间设置为整数,单位为秒,最大不能超过输入时间,若超过了答题时间未答题,则提示:时间已到;

    6、程序可以设置皮肤功能,可以改变界面的颜色。

    三、结对同学及分工 

    姓名:陈世炯

    学号:201606120006

    博客园地址:https://www.cnblogs.com/chenshijiong

    扩展功能:

    1)用户答题结束以后,程序可以显示用户答题所用的时间;

    2)用户可以一次答题不能超过5个题目,若超过5个题目,答题结束可以显示用户答错的题目个数和答对的题目个数并统计分数;

    3)程序可以设置答题时间,时间设置为整数,单位为秒,若超过了答题时间未答题,则提示:时间已到。

    本人扩展功能:

    1)程序可以出带括号的正整数四则运算,不支持分数,除法保留两位小数;

    2)程序可以设置皮肤功能,可以改变界面的颜色;

    3)用户在答题时,需要用户输入用户名

    四、估计所需时间

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

    五、开发环境

     

     编程软件:Intellij IDEA

    六、本次作业总结及感悟

    1)除法保留两位小数

          在做除法保留小数时,首先遇到的问题是,用java语言不知道怎么用;解决方法是通过网上其他的博客上参考其他人的做法,从而实现了除法的时候可以保留两位小数。

         

     String formula=("("+x+"")+operator+(""+y)+")"+operator1+(""+z)+"=";
                    DecimalFormat df=new DecimalFormat("0.00");
                    String nums=df.format((float)(x+y)/z);
                    result=Float.parseFloat(nums);
                    map.put(formula, result);

    2)改变界面颜色

         在做改变界面颜色初始打算是在JFrame中的一个面板创建按钮,直接点击按钮可以改变窗体颜色,但开始的方法不对,需要自己一种一种颜色设置好;之后再网上看到其他的程序员用了一个方法,直接生成一个颜色选择面板,可以提供javaJFrame里面所有提供的颜色。

    调色代码如下:

     JButton btnChengColor = new JButton("背景颜色");
            btnChengColor.setBounds(30, 300,30, 30);
            btnChengColor.setPreferredSize(new Dimension(100,80));
            btnChengColor.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color ch = JColorChooser.showDialog(f, "颜色选择器",
                            f.getBackground());
    
                    if (ch != null) {
                        f.getContentPane().setBackground(ch);
                        pan1.setBackground(ch);
                        pan1.repaint();
                        pan2.setBackground(ch);
                        pan2.repaint();
                        pan3.setBackground(ch);
                        pan3.repaint();
                        pan4.setBackground(ch);
                        pan4.repaint();
                        pan5.setBackground(ch);
                        pan5.repaint();
                        pan6.setBackground(ch);
                        pan6.repaint();
                        f.getContentPane().repaint();
                    }
                }
            });
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            pan7.add(btnChengColor);
            pan7.setBounds(470, 350, 110, 90);
            f.add(pan7);

    改变窗体颜色时,点击按钮弹出调色面板如图:

    3)需要用户输入用户名和密码登录系统

         设置好系统用户名为:Operation,密码为:123。可以直接登录进去,进行相应操作!

    登录界面如图:

    登录成功后弹框如图:

    七、结对工作照片

     

    八、附上源码

    源码如下:欢迎指正

      1 package SZYS;
      2 import javafx.concurrent.Task;
      3 
      4 import java.awt.*;
      5 import java.awt.event.*;
      6 import java.text.DecimalFormat;
      7 import java.text.ParseException;
      8 import java.text.SimpleDateFormat;
      9 import java.util.*;
     10 import java.util.Timer;
     11 import javax.swing.*;
     12 
     13 import static javafx.application.Platform.exit;
     14 
     15 public class Operation extends JFrame{
     16     public static void main(String[] args)
     17     {
     18         login();
     19     }
     20     //定义全局的分数
     21     static int score=0;
     22     //定义全局的value结果0
     23     static float currValue;
     24     // 定义全局运算式的字符串
     25     static String currString;
     26     //定义全局时间
     27     static String currTime;
     28     //定义全局题目数
     29     static int currNums=0;
     30 
     31     /**答题界面**/
     32     static JLabel nameTest3=new JLabel("");
     33     public static void exercise()
     34     {
     35         JFrame f=new JFrame();
     36         f.setTitle("在线答题系统(保留两位小数)");
     37         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     38         f.setExtendedState(JFrame.MAXIMIZED_BOTH);
     39         f.setVisible(true);
     40         //设置窗口的大小和位置
     41         f.setSize(600,500);
     42         f.setLocation(400,100);
     43         f.setLayout(null);
     44         JPanel pan1=new JPanel();
     45         JLabel name1=new JLabel("7/7=");
     46         pan1.add(name1);
     47         JTextField  nameTest1=new JTextField(15);
     48         nameTest1.setBounds(10, 10, 180, 100);
     49         //nameTest1.setPreferredSize(new Dimension(180,100));
     50         pan1.add(nameTest1);
     51         pan1.setBounds(10, 10, 200, 120);
     52         f.add(pan1);
     53         JPanel pan2=new JPanel();
     54         JLabel name2=new JLabel("请输入秒数:");
     55         pan2.add(name2);
     56         JTextField nameTest2=new JTextField(15);
     57         nameTest2.setBounds(300, 10, 180, 100);
     58        //    nameTest2.setPreferredSize(new Dimension(180,100));
     59         pan2.add(nameTest2);
     60         pan2.setBounds( 300,10, 200, 120);
     61         f.add(pan2);
     62 
     63         JPanel pan3=new JPanel();
     64         pan3.setLayout(null);
     65 
     66         nameTest3.setBounds(10, 60, 480, 200);
     67         nameTest3.setPreferredSize(new Dimension(300,100));
     68         pan3.add(nameTest3);
     69         pan3.setBounds( 10,60, 500, 220);
     70         f.add(pan3);
     71         JPanel pan4 = new JPanel();
     72         //    pan4.setLayout(null);
     73         JButton btnStart=new JButton("开始");
     74         btnStart.setBounds(30, 300,30, 30);
     75         btnStart.setPreferredSize(new Dimension(100,80));
     76 
     77 
     78         btnStart.addActionListener(new ActionListener() {
     79             @Override
     80             public void actionPerformed(ActionEvent e) {
     81                 Map<String, Float> map = new HashMap<String, Float>();
     82                 map = operation();
     83                 Set set = map.keySet();
     84                 Iterator iter = set.iterator();
     85                 String key = (String) iter.next();
     86                 float value = map.get(key);
     87                 if (btnStart.getText().equals("开始 1")) {
     88 //                    currNums+=1;
     89 //                    btnStart.setText("下一题");
     90 //                    name1.setText(key);
     91 //                    System.out.println("value:"+value);
     92 //                    currValue=value;
     93 //                    currString=key;
     94 //                    map.clear();
     95                 } else {
     96                     String ttime = nameTest2.getText();
     97                     int ttime3 =Integer.parseInt(ttime);
     98                     Timer timer = new Timer();// 实例化Timer类
     99                     timer.schedule(new TimerTask() {
    100                         public void run() {
    101                             nameTest3.setFont(new Font("宋体", Font.PLAIN, 20));
    102                             nameTest3.setText("你本次用时超过"+ttime3+"秒");
    103                         }
    104                     }, ttime3*1000);// 这里百毫秒
    105                     btnStart.setText("下一题");
    106                     currNums += 1;
    107                     if (currNums == 5) {
    108                         endTime();
    109                     }
    110 
    111                     name1.setText(key);
    112                     System.out.println("value:" + value);
    113                     currValue = value;
    114                     currString = key;
    115                     map.clear();
    116                     nameTest1.addKeyListener(new KeyAdapter() {
    117                         public void keyPressed(KeyEvent e) {
    118                             //按回车键执行相应操作;
    119                             if (e.getKeyChar() == KeyEvent.VK_ENTER) {
    120                                 String answerStr = nameTest1.getText();
    121                                 float answer = Float.parseFloat(answerStr);
    122                                 //判断正误,进行加分,并显示
    123                                 System.out.println("answer:" + answer);
    124                                 System.out.println("value:" + currValue);
    125                                 if (answer == currValue) {
    126                                     score += 10;
    127                                     nameTest3.setFont(new Font("宋体", Font.PLAIN, 20));
    128                                     nameTest3.setText("本题为:" + currString + "" + currValue + " ||  您的回答正确 || 当前分数:" + score);
    129                                     nameTest1.setText("");
    130                                 } else {
    131                                     nameTest3.setFont(new Font("宋体", Font.PLAIN, 20));
    132                                     nameTest3.setText("本题为:" + currString + "" + currValue + " ||  您的回答错误 || 当前分数:" + score);
    133                                 }
    134                             }
    135                         }
    136 
    137                         ;
    138                     });
    139                 }
    140             }
    141         });
    142 
    143         pan4.add(btnStart);
    144         pan4.setBounds(40, 350, 110, 90);
    145         f.add(pan4);
    146         JPanel pan5 = new JPanel();
    147         //    pan4.setLayout(null);
    148         JButton btnStart1=new JButton("计时");
    149         btnStart1.setBounds(30, 300,30, 30);
    150         btnStart1.setPreferredSize(new Dimension(100,80));
    151         btnStart1.addActionListener(new ActionListener() {
    152             @Override
    153             public void actionPerformed(ActionEvent e) {
    154                 if(btnStart1.getText().equals("计时")){
    155                     btnStart1.setText("正在计时...");
    156                     nameTest3.setFont(new Font("宋体", Font.PLAIN, 20));
    157                     nameTest3.setText("              计时开始,请认真答题");
    158                     //获取当前时间
    159                     SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    160                     currTime=df.format(new Date());
    161                 }
    162             }
    163         });
    164 
    165         Timer timer = new Timer();// 实例化Timer类
    166         timer.schedule(new TimerTask() {
    167             public void run() {
    168             }
    169         }, 50000);// 这里百毫秒
    170 
    171 
    172         pan5.add(btnStart1);
    173         pan5.setBounds(190, 350, 110, 90);
    174         f.add(pan5);
    175         JPanel pan6 = new JPanel();
    176         //    pan4.setLayout(null);
    177         JButton btnStart2=new JButton("结束");
    178         btnStart2.setBounds(30, 300,30, 30);
    179         btnStart2.setPreferredSize(new Dimension(100,80));
    180         btnStart2.addActionListener(new ActionListener() {
    181             @Override
    182             public void actionPerformed(ActionEvent e) {
    183                 //计算用时
    184                endTime();
    185             }
    186         });
    187 
    188         pan6.add(btnStart2);
    189         pan6.setBounds(340, 350, 110, 90);
    190         f.add(pan6);
    191 
    192         JPanel pan7 = new JPanel();
    193         JButton btnChengColor = new JButton("背景颜色");
    194         btnChengColor.setBounds(30, 300,30, 30);
    195         btnChengColor.setPreferredSize(new Dimension(100,80));
    196         btnChengColor.addActionListener(new ActionListener() {
    197             @Override
    198             public void actionPerformed(ActionEvent e) {
    199                 Color ch = JColorChooser.showDialog(f, "颜色选择器",
    200                         f.getBackground());
    201 
    202                 if (ch != null) {
    203                     f.getContentPane().setBackground(ch);
    204                     pan1.setBackground(ch);
    205                     pan1.repaint();
    206                     pan2.setBackground(ch);
    207                     pan2.repaint();
    208                     pan3.setBackground(ch);
    209                     pan3.repaint();
    210                     pan4.setBackground(ch);
    211                     pan4.repaint();
    212                     pan5.setBackground(ch);
    213                     pan5.repaint();
    214                     pan6.setBackground(ch);
    215                     pan6.repaint();
    216                     f.getContentPane().repaint();
    217                 }
    218             }
    219         });
    220         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    221         f.setVisible(true);
    222         pan7.add(btnChengColor);
    223         pan7.setBounds(470, 350, 110, 90);
    224         f.add(pan7);
    225 
    226 
    227     }
    228 
    229 
    230     /**生成四则运算**/
    231     public static Map<String,Float> operation()
    232     {
    233         Map<String,Float> map=new HashMap<String, Float>();
    234         String[] operators={"+","-","x","/"};
    235         int x=(int)(Math.random()*100);
    236         int y=(int)(Math.random()*100);
    237         int z=(int)(Math.random()*100);
    238         int index=(int)(Math.random()*4);
    239         int index1=(int)(Math.random()*4);
    240         while(index==4){
    241             index=(int)Math.random()*4;
    242         }
    243         String operator=operators[index];
    244         float result=0;
    245         if(operator.equals("+"))
    246         {
    247             while(index1==4){
    248                 index1=(int)Math.random()*4;
    249             }
    250 
    251             String operator1=operators[index1];
    252             if(operator1.equals("+"))
    253             {
    254                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    255                 result=x+y+z;
    256                 map.put(formula, result);
    257             }
    258             else if (operator1.equals("-"))
    259             {
    260                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    261                 result=x+y-z;
    262                 map.put(formula, result);
    263             }
    264             else if (operator1.equals("x"))
    265             {
    266                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    267                 result=x+y*z;
    268                 map.put(formula, result);
    269             }
    270             else
    271             {
    272                 String formula=("("+x+"")+operator+(""+y)+")"+operator1+(""+z)+"=";
    273                 DecimalFormat df=new DecimalFormat("0.00");
    274                 String nums=df.format((float)(x+y)/z);
    275                 result=Float.parseFloat(nums);
    276                 map.put(formula, result);
    277             }
    278 
    279         }
    280         else if(operator.equals("-"))
    281         {
    282             while(index1==4){
    283                 index1=(int)Math.random()*4;
    284             }
    285             String operator1=operators[index1];
    286             if (operator1.equals("+"))
    287             {
    288                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    289                 result=x-y+z;
    290                 map.put(formula, result);
    291             }
    292             else if (operator1.equals("-"))
    293             {
    294                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    295                 result=x-y-z;
    296                 map.put(formula, result);
    297             }
    298             else if (operator1.equals("x"))
    299             {
    300                 String formula=(+x+"")+operator+(""+y)+operator1+(""+z)+"=";
    301                 result=x-y*z;
    302                 map.put(formula, result);
    303             }
    304             else
    305             {
    306                 String formula=("("+x+"")+operator+(""+y+")")+operator1+(""+z)+"=";
    307                 DecimalFormat df=new DecimalFormat("0.00");
    308                 String nums=df.format((float)(x-y)/z);
    309                 result=Float.parseFloat(nums);
    310                 map.put(formula, result);
    311             }
    312 
    313         }
    314         else if(operator.equals("x"))
    315         {
    316             while(index1==4){
    317                 index1=(int)Math.random()*4;
    318             }
    319             String operator1=operators[index1];
    320             if (operator1.equals("+"))
    321             {
    322                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    323                 result=x*y+z;
    324                 map.put(formula, result);
    325             }
    326             else if(operator1.equals("-"))
    327             {
    328                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    329                 result=x*y-z;
    330                 map.put(formula, result);
    331             }
    332             else if (operator1.equals("x"))
    333             {
    334                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    335                 result=x*y*z;
    336                 map.put(formula, result);
    337             }
    338             else
    339             {
    340                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    341                 DecimalFormat df=new DecimalFormat("0.00");
    342                 String nums=df.format((float)x*y/z);
    343                 result=Float.parseFloat(nums);
    344                 map.put(formula, result);
    345             }
    346 
    347         }
    348         else
    349         {
    350             while(index1==4){
    351                 index1=(int)Math.random()*4;
    352             }
    353             String operator1=operators[index1];
    354             if (operator1.equals("+"))
    355             {
    356                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    357                 DecimalFormat df=new DecimalFormat("0.00");
    358                 String nums=df.format((float)x/y+z);
    359                 result=Float.parseFloat(nums);
    360                 map.put(formula, result);
    361             }
    362             else if (operator.equals("-"))
    363             {
    364                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    365                 DecimalFormat df=new DecimalFormat("0.00");
    366                 String nums=df.format((float)x/y-z);
    367                 result=Float.parseFloat(nums);
    368                 map.put(formula, result);
    369             }
    370             else if (operator1.equals("x"))
    371             {
    372                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    373                 DecimalFormat df=new DecimalFormat("0.00");
    374                 String nums=df.format((float)x/y*z);
    375                 result=Float.parseFloat(nums);
    376                 map.put(formula, result);
    377             }
    378             else
    379             {
    380                 String formula=(x+"")+operator+(""+y)+operator1+(""+z)+"=";
    381                 DecimalFormat df=new DecimalFormat("0.00");
    382                 String nums=df.format((float)x/y/z);
    383                 result=Float.parseFloat(nums);
    384                 map.put(formula, result);
    385             }
    386 
    387         }
    388         return map;
    389     }
    390     /**登录跳转方法**/
    391     public static  void login()
    392     {
    393         JFrame f=new JFrame();
    394         f.setTitle("系统登录界面");
    395         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    396         f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    397         f.setVisible(true);
    398         //设置窗口的大小和位置
    399         f.setSize(400,400);
    400         f.setLocation(420,120);
    401         Container con=f.getContentPane();//生成一个容器
    402         con.setLayout(new GridLayout(7,1));
    403         //生成一个新的版面
    404         JPanel pan1=new JPanel();
    405         JLabel title=new JLabel("欢迎登陆本系统");
    406         title.setFont(new Font("宋体",Font.BOLD, 20));
    407         pan1.add(title);
    408         con.add(pan1);
    409         //最上面的登陆文字
    410        //生成一个新的版面
    411 
    412         JPanel pan2=new JPanel();
    413         JLabel name=new JLabel("用户名");
    414         pan2.add(name);
    415         JTextField nameTest=new JTextField(15);
    416         pan2.add(nameTest);
    417         con.add(pan2);
    418         f.setSize(500,600);
    419         //用户名及其文本框放置在第二个版面上
    420 
    421         //生成一个新的版面
    422         JPanel pan3=new JPanel();
    423         JLabel pass = new JLabel("密码");
    424         pan3.add(pass);
    425         JPasswordField password=new JPasswordField(15);
    426         password.setEchoChar('*');
    427         pan3.add(password);
    428         con.add(pan3);
    429          //密码及其密码域放在第三个版面上
    430         // System.out.println(username+"   "+userPassword);
    431         JPanel pan4 = new JPanel();
    432         JButton b_log=new JButton("登陆");
    433         b_log.addActionListener(new ActionListener() {
    434             public void actionPerformed(ActionEvent e) {
    435 
    436                 //获取用户名和密码,进行校验
    437                 String myUsername=nameTest.getText();
    438                 String myPassword=password.getText();
    439                 if(myUsername.equals("Operation")&&myPassword.equals("123")){
    440                     JOptionPane.showMessageDialog(null, "登陆成功!");
    441                     exercise();
    442                     //System.exit(0);
    443                 }
    444                 else
    445                 {
    446                     JOptionPane.showMessageDialog(null, "账号或密码错误!");
    447                     nameTest.setText("");
    448                     password.setText("");
    449                 }
    450             }
    451         });
    452         pan4.add(b_log);
    453         JButton b_exit=new JButton("退出");
    454         pan4.add(b_exit);
    455         con.add(pan4);
    456         f.setSize(500,650);
    457         //登陆和退出这两个按钮放在第四个版面上
    458         JPanel pan5 = new JPanel();
    459         con.add(pan5);
    460         JPanel pan6 = new JPanel();
    461         con.add(pan6);
    462         JPanel pan7 = new JPanel();
    463         con.add(pan7);
    464         //空白版面
    465     }
    466 
    467     public  static void endTime(){
    468         SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    469         String endTime=df.format(new Date());
    470         try {
    471             long start =df.parse(currTime).getTime();
    472             long end=df.parse(endTime).getTime();
    473             int minutes = (int) ((end - start)/(1000 ));
    474             nameTest3.setFont(new Font("宋体", Font.PLAIN, 20));
    475             nameTest3.setText("时间:"+minutes+"秒  ||"+" 一共计算了"+currNums+"道题  ||  总得分:"+score);
    476         } catch (ParseException e1) {
    477             e1.printStackTrace();
    478         }
    479     }
    480 }
  • 相关阅读:
    2020春Python程序设计_练习1
    热词分析——性能战术
    《淘宝网》质量属性
    架构师应该如何工作?
    寒假学习(十四)
    寒假学习(十三)
    灭霸冲刺(3)
    灭霸冲刺(2)
    灭霸冲刺(1)
    灭霸计划会议
  • 原文地址:https://www.cnblogs.com/ShaoJingWen/p/9845332.html
Copyright ? 2011-2022 开发猿


http://www.vxiaotou.com