博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第5.2次作业
阅读量:4522 次
发布时间:2019-06-08

本文共 5218 字,大约阅读时间需要 17 分钟。

这次的结对伙伴是:03游金浪,博客:http://www.cnblogs.com/santokim/

 

import java.awt.*; import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import javax.swing.*; public  class aaa extends JFrame implements ActionListener{    JLabel L1=new JLabel("题目:");    JLabel L2=new JLabel("输入题目个数:");     JLabel L3=new JLabel("输入答案");    JLabel L4=new JLabel("        ");     JLabel L5=new JLabel("        ");    JTextField T1=new JTextField(6);    JTextField T2=new JTextField(6);    JButton B1=new JButton("开始");    JButton B2=new JButton("重新开始");    JButton B3=new JButton("下一题");    JButton B4=new JButton("提交");    JPanel p1=new JPanel();    int n=0,ri,m=0,p;    int a[]=new int[100];    int b[]=new int[100];    String c[]=new String[100];    String ch[]={"+","-","*","/"};    public static void main(String[] args) {        new aaa();                // TODO Auto-generated method stub    }    aaa()    {                    this.setTitle("四则运算");            this.setSize(300,350);            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);            this.add(p1);             p1.setLayout(null);            L2.setBounds(50, 20, 90, 20);            p1.add(L2);            T2.setBounds(150, 20, 90, 30);            p1.add(T2);           L1.setBounds(50, 70, 90, 30);            p1.add(L1);            L4.setBounds(150, 70, 90, 30);            p1.add(L4);            L3.setBounds(50,120, 90, 30);            p1.add(L3);            T1.setBounds(150, 120, 90, 30);            p1.add(T1);            T1.setEnabled(false);            B1.setBounds(50, 170, 90, 30);            p1.add(B1);            B1.addActionListener(this);            B2.setBounds(150, 170, 90, 30);            p1.add(B2);            B2.setEnabled(false);            B2.addActionListener(this);            B3.setBounds(50,220,90,30);            p1.add(B3);            B3.setEnabled(false);            B3.addActionListener(this);            B4.setBounds(150, 220,90,30);            p1.add(B4);            B4.setEnabled(false);            B4.addActionListener(this);            L5.setBounds(50, 270, 180, 30);            p1.add(L5);            this.setVisible(true);            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体                 B1.addActionListener(this);    }    public void actionPerformed(ActionEvent e){        if(e.getSource()==B1){            B1.setEnabled(false);            B2.setEnabled(true);            B3.setEnabled(true);            B4.setEnabled(true);            T2.setEnabled(false);            T1.setEnabled(true);            L5.setText(" ");            m=0;            ri=0;            n=(int) Double.parseDouble(T2.getText());            if(n>5||n<1)            {                JOptionPane.showMessageDialog(null,"输入的个数不能大于5或者小于1","注意",JOptionPane.INFORMATION_MESSAGE);            }            else begin();                    }        if(e.getSource()==B2){            B1.setEnabled(true);            B2.setEnabled(false);            B3.setEnabled(false);            B4.setEnabled(false);            T2.setEnabled(true);            T1.setText("");            T2.setText("");        }        if(e.getSource()==B3){                begin();                T1.setText("");                B4.setEnabled(true);                L5.setText(" ");        }        if(e.getSource()==B4){            answer();            B4.setEnabled(false);            if(m==n){                B2.setEnabled(true);                B3.setEnabled(false);                B4.setEnabled(false);                T1.setEditable(false);                JOptionPane.showMessageDialog(null,"答对了"+ri+"题,答错了"+(n-ri)+"题","成绩",JOptionPane.INFORMATION_MESSAGE);            }        }    }    void begin(){        a[m]=(int)(Math.random()*200-100);        b[m]=(int)(Math.random()*200-100);        c[m]=ch[(int)(Math.random()*3)];        while(b[m]==0&&c[m]==ch[3]){            b[m]=(int)(Math.random()*200-100);        }        if(a[m]>=0&&b[m]>=0){        L4.setText(String.valueOf(a[m])+c[m]+String.valueOf(b[m])+"=");        }        if(a[m]<0&&b[m]>=0){            L4.setText("("+String.valueOf(a[m])+")"+c[m]+String.valueOf(b[m])+"=");            }        if(a[m]>=0&&b[m]<0){            L4.setText(String.valueOf(a[m])+c[m]+"("+String.valueOf(b[m])+")"+"=");            }        if(a[m]<0&&b[m]<0){            L4.setText("("+String.valueOf(a[m])+")"+c[m]+"("+String.valueOf(b[m])+")"+"=");            }    }    void answer(){        cal re=new cal();        double an=Double.parseDouble(T1.getText());        if(re.calc(a[m], b[m], c[m])==an){            L5.setText("答案正确!");            ri++;        }        else{            L5.setText("答案错误!正确答案是"+re.calc(a[m], b[m], c[m]));        }        m++;    }}

 

public class cal {    int calc(int a,int b,String c){        int d = 0;        if(c.equals("+")) d=add(a,b);        if(c.equals("-")) d=minius(a,b);        if(c.equals("*")) d=mult(a,b);        if(c.equals("/")) d=divi(a,b);        return d;    }    int add(int a,int b){        return a+b;    }     int minius(int a,int b){        return a-b;    }    int mult(int a,int b){        return a*b;    }    int divi(int a,int b){        return a/b;    }}

 

单元测试:

转载于:https://www.cnblogs.com/cds94/p/4488569.html

你可能感兴趣的文章
jQuery ajax error函数(交互错误信息的获取)
查看>>
Gson解析Json数组
查看>>
Lintcode: Fast Power
查看>>
Pocket Gem OA: Log Parser
查看>>
枚举也能直接转换为对应的数值输出
查看>>
angularjs1-7,供应商
查看>>
BitSet
查看>>
Spring常用注解,自动扫描装配Bean
查看>>
(转载)深入理解WeakHashmap
查看>>
JAVA中的数组
查看>>
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>
RabbitMQ学习之Publish/Subscribe(3)
查看>>
[SCOI2010]生成字符串
查看>>
JLOI2015 城池攻占
查看>>