实现简单的四则运算

任务

  • 能自动生成小学四则运算题目,并且能进行批改。
  • 主要思想:random函数生成两个随机,运算符号用字符数组存储,通过随机取下标实现运算符号的随机性,用Java常用的组件及事件处理设计窗口和功能实现。

 

PSP表格

 

PSP

 

实际耗时(分钟)

Planning

计划

30

Estimate

估计这个任务需要多少时间

20

Analysis

需求分析

10

Design Spec

生成设计文档

0

Design Review

设计复审(和同事审核设计文档)

0

Coding Standerd

代码规范(为目前的开发制定合适的规范)

10

Design

具体设计

30

Coding

具体编码

180

Code Review

代码复审

10

Text

测试(自测,修改代码,提交修改)

30

Reporting

报告

10

Text Report

测试报告

10

Size Measurement

计算工作量

10

Postmortem & Process Improvement Plan

事后总结,并提出过程改进计划

20

Sum

合计

370

 

代码实现

  1 import javax.swing.*; //1000以内的四则运算测试-用一个监视器
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import java.util.Random;
  5 import java.text.DecimalFormat;
  6 public class Calculation
  7 {
  8     public static void main(String[] args) {
  9         Wind win = new Wind();
 10         win.setTitle("1000以内的四则运算测试");
 11         win.setBounds(0,0,500,200);
 12      }
 13 }
 14 
 15 class Wind extends JFrame 
 16 {
 17     Wind(){
 18         setLayout(null);
 19         Container con = getContentPane();
 20         con.setLayout(null);
 21         con.setBackground(Color.pink);
 22         init();
 23         setBounds(10,10,460,360);
 24         setVisible(true);
 25         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 26     }
 27     void init(){
 28         JButton button1 = new JButton("获得题目");
 29         JTextArea textshow = new JTextArea();
 30         JButton button2 = new JButton("确认答案");
 31         JTextField text = new JTextField(20);
 32         JButton button3 = new JButton("重置");
 33         JButton button4 = new JButton("正确答案");
 34         textshow.setFont(new Font("楷体",Font.BOLD,20));    //修改字体样式
 35         text.setFont(new Font("楷体",Font.BOLD,20));
 36         Listen listen = new Listen();
 37         listen.setJTextArea(textshow);
 38         listen.setJTextField(text);
 39         button1.addActionListener(listen);
 40         button2.addActionListener(listen);
 41         button3.addActionListener(listen);
 42         button4.addActionListener(listen);
 43         button1.setBounds(10,10,100,25);
 44         textshow.setBounds(110,10,200,25);
 45         button2.setBounds(10,40,100,25);
 46         text.setBounds(110,40,200,25);
 47         button3.setBounds(320,10,100,25);
 48         button4.setBounds(320,40,100,25);
 49         add(button1);
 50         add(textshow);
 51         add(button2);
 52         add(text);
 53         add(button3);
 54         add(button4);
 55     }
 56 }
 57         
 58 class Listen implements ActionListener
 59 {   
 60     JTextArea textshow;
 61     JTextField text;
 62     int numberOne;
 63     int numberTwo;
 64     double inputnumber;
 65     String random="";
 66     int index;
 67     double sum;
 68     String [] doc= {"+","-","*","/"};
 69     public void setJTextArea(JTextArea textshow){
 70         this.textshow=textshow;
 71       }
 72     
 73     public void setJTextField(JTextField text){
 74         this.text = text;
 75        }
 76     public void setnumberOne(int numberOne)
 77         {
 78             this.numberOne = numberOne;
 79         }
 80     public void setnumberTwo(int numberTwo)
 81         {
 82             this.numberTwo = numberTwo;
 83         }
 84     
 85     public void actionPerformed(ActionEvent e){
 86         String str = e.getActionCommand();
 87         if(str.equals( "获得题目"))
 88       {
 89         numberOne=(int)(Math.random()*1000);
 90         numberTwo=(int)(Math.random()*1000);
 91         setnumberOne(numberOne);
 92         setnumberTwo(numberTwo);
 93         index=(int)(Math.random()*doc.length);
 94         random=doc[index];
 95         textshow.setText(numberOne+random+numberTwo);
 96         if(random=="+")
 97               sum = numberOne+numberTwo;
 98           if(random=="-")
 99               sum = numberOne-numberTwo;
100           if(random=="*")
101               sum = numberOne*numberTwo;
102           if(random=="/") {
103               JOptionPane.showMessageDialog(null, "提示:四舍五入,保留小数点后两位!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
104               sum = (double)numberOne/numberTwo;
105               DecimalFormat df=new DecimalFormat("#.00");//保留小数点后两位
106               String ssum=df.format(sum);
107               sum=Double.parseDouble(ssum);
108           }
109       }
110       if(str.equals( "确认答案"))
111       {
112           try{
113            inputnumber=Double.parseDouble(text.getText());
114            }
115           catch(NumberFormatException event){}
116             
117             if(inputnumber==sum)
118                 {
119                 JOptionPane.showMessageDialog(null, "答案正确,你很棒!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
120                 }
121            else{
122                 JOptionPane.showMessageDialog(null, "答案错误,要努力哦!","消息对话框",JOptionPane.WARNING_MESSAGE);
123                }    
124       }
125       if(str.equals("重置")){
126          textshow.setText("");
127          text.setText("");
128       }
129       if(str.equals("正确答案")){
130          JOptionPane.showMessageDialog(null, "答案正确是:"+sum,"消息对话框",JOptionPane.INFORMATION_MESSAGE);
131       }
132     }
133 
134 }

 

 测试结果

 

 

最后总结:继续加油!!! (-^〇^-) 

 

posted @ 2021-09-12 23:04  木彳  阅读(67)  评论(0编辑  收藏  举报