结对子作业 四则运算 V2.0

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;

import javax.swing.*;
public class MainJFrame extends JFrame {  
    JButton b1=new JButton("切换皮肤");JButton b2=new JButton("GOGOGO!");JButton b3=new JButton("开始/下一题");
    int xz, cc,count,max,min=1,jc,sum1,dt=0,tm=0,tm1=0,zs1,zs2,fs1,fs2,fs3,fs4;
    char ssfh[]={'+','-','*','/'};
    int fh;
    String Sum1,Sum2;
    long star,end;
    JLabel l1=new JLabel("   题目");JLabel l2=new JLabel("                  得数");
    JTextField t1=new JTextField(10);JTextField t2=new JTextField(8);
        String [] path={
        "0.jpg",
        "1.jpg",
        "2.jpg"};
    public MainJFrame(int count1,int cc1,int xz1,int max1,long star1) {  
        count=count1;
        cc=cc1;
        xz=xz1;
        max=max1;
        star=star1;
        //设置大小  
        setSize(500, 400);  
        //设置位置  
        setLocation(200, 50);  
        //背景图片的路径。(相对路径或者绝对路径。本例图片放于"java项目名"的文件下)  
        // 背景图片  
        ImageIcon background = new ImageIcon(path[1]);  
        // 把背景图片显示在一个标签里面  
        JLabel label = new JLabel(background);  
        // 把标签的大小位置设置为图片刚好填充整个面板  
        label.setBounds(0, 0, this.getWidth(), this.getHeight());  
        // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明  
        JPanel imagePanel = (JPanel) this.getContentPane();  
        imagePanel.setOpaque(false);  
        // 把背景图片添加到分层窗格的最底层作为背景  
        imagePanel.setLayout(new FlowLayout(FlowLayout.CENTER,20,100));
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));  
        imagePanel.add(l1);imagePanel.add(t1);imagePanel.add(l2);imagePanel.add(t2);imagePanel.add(b1);imagePanel.add(b3);imagePanel.add(b2);
        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(xz==1)
                {
                    if(tm1<count){
                    tm1++;
                    zs1=new  Random().nextInt(max) + min;
                    zs2=new  Random().nextInt(max) + min;
                    fh=new Random().nextInt(4);
                    while(fh==3&&zs2==0){
                        zs2=new  Random().nextInt(max) + min;
                    }
                    Zhengshu zhengshu=new Zhengshu(zs1,fh,zs2);
                    if(fh==0){
                    t1.setText(zhengshu.getJiaf()); 
                    zhengshu.setJiaf(zs1, zs2);
                    }
                    else if(fh==1){
                        t1.setText(zhengshu.getJianf()); 
                        zhengshu.setJianf(zs1, zs2);
                    }
                    else if(fh==2){
                        t1.setText(zhengshu.getChengf());
                        zhengshu.setChengf(zs1, zs2);
                    }
                    else if(fh==3){
                        t1.setText(zhengshu.getChuf());
                        zhengshu.setChuf(zs1, zs2);
                        }
                    }
                }
                else if(xz==2){
                    if(tm1<count){
                        tm1++;
                    fs1=new  Random().nextInt(max) + min;
                    fs2=new  Random().nextInt(max) + min;
                    fs3=new  Random().nextInt(max) + min;
                    fs4=new  Random().nextInt(max) + min;
                    Fenshu fenshu=new Fenshu();
                    fh=new Random().nextInt(3);
                    t1.setText(fenshu.suanshi(fs1, fs2, fs3, fs4, fh));
                    }
                }
                else{
                    if(tm1<count){
                    
                    tm1++;
                    jc=new  Random().nextInt(max) + min;t1.setText("("+ tm1 +")"+jc+"!=");                   
                    
                    }
                }
               
                }
            });
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(xz==1)
                {
                    if(tm<count)
                    {
                    Zhengshu zhengshu=new Zhengshu();
                        if(fh==0){ 
                        zhengshu.setJiaf(zs1, zs2);
                        }
                        else if(fh==1){
                            zhengshu.setJianf(zs1, zs2);
                        }
                        else if(fh==2){
                            zhengshu.setChengf(zs1, zs2);
                        }
                        else if(fh==3){
                            zhengshu.setChuf(zs1, zs2);
                            }
                        if(fh==3)
                            {Sum2=t2.getText();
                            Sum1=zhengshu.getsum(zs1,zs2);
                            if(Sum1.equals(Sum2))
                                dt++;
                            
                            }
                        else{
                            if((sum1=Integer.valueOf(t2.getText()))==zhengshu.getSum())
                            dt++;                    
                        }
                        
                    tm++;
                    t2.setText(null);
                //    System.out.print(tm+"\t"+zs1+"\t"+fh+"\t"+zs2+"\t"+dt+"\t"+zhengshu.getSum()+"\t"+Sum1+"\n");
                    }
                    if(tm==count){
                        end=System.currentTimeMillis()/1000;
            //            System.out.print("\t"+end+"\t"+star+"\t"+(end-star));
                        JOptionPane.showMessageDialog(rootPane, "一共"+count+"道题,答对"+dt+"道题!\t" +"得分"+(100/count*dt)+"用时"+(end-star)+"s");
                    }
                
                    
                }
                else if(xz==2){
                    if(tm<count)
                    {
                        Fenshu fenshu=new Fenshu();
                        Sum1=fenshu.answer(fs1, fs2, fs3, fs4, fh);
                        Sum2=t2.getText();
                        if(Sum1.equals(Sum2))
                        {
                            dt++;
                        }
                        tm++;
                        t2.setText(null);
                //        System.out.print(tm+"\t"+dt+"\t"+fenshu.answer(fs1, fs2, fs3, fs4, fh)+"\t"+Sum2+"\t"+fh+"\n");
                        if(tm==count){
                            end=System.currentTimeMillis()/1000;
            //                System.out.print("\t"+end+"\t"+star+"\t"+(end-star));
                            JOptionPane.showMessageDialog(rootPane, "一共"+count+"道题,答对"+dt+"道题!\t" +"得分"+(100/count*dt)+"用时"+(end-star)+"s");
                        }
                    }
                }
                else{
                    if(tm<count)
                    {
                    Jiecheng jiecheng=new Jiecheng(jc);
                    if((sum1=Integer.valueOf(t2.getText()))==jiecheng.getJieguo())
                    {
                        dt++;
                    }
                    tm++;
                    t2.setText(null);
                    System.out.print(tm+"\t"+jc+"!\t"+dt+"\t"+jiecheng.getsum()+"\t"+sum1+"\n");
                    }
                    if(tm==count){
                        end=System.currentTimeMillis()/1000;
            //            System.out.print("\t"+end+"\t"+star+"\t"+(end-star));
                        JOptionPane.showMessageDialog(rootPane, "一共"+count+"道题,答对"+dt+"道题!\t" +"得分"+(100/count*dt)+"用时"+(end-star)+"s");
                    }
                }
               
                }
                });
        //设置可见  
        setVisible(true);  
        //点关闭按钮时退出  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }  

 
    
    public static void main(String[] args) {  
        Baseframe frame=new Baseframe();
        
    }  
}  
    class Baseframe extends JFrame                                                                        //基础界面
    {
        public Baseframe(){
            super("四则运算 V2.0"); 
            this.getContentPane().add(new p1(),"Center");
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            this.setSize(500, 400);
            this.setLocation(200, 50);
            this.setVisible(true);
            
        }

        public static void close() {
            // TODO Auto-generated method stub
            
        }
    }
    class p1 extends JPanel{
        int xz, cc,count,max,min=1;long star;
        JLabel l1=new JLabel("                  输入题目数");JLabel l2=new JLabel("                  是否有乘除法0/1");JLabel l3=new JLabel("                  数值范围是(1~)");JLabel l4=new JLabel("   1:整数运算.2:分数运算.3:阶乘运算.");
        JLabel l5=new JLabel();
        JButton b1=new JButton("确定");
        JTextField t1=new JTextField(8);JTextField t2=new JTextField(8);JTextField t3=new JTextField(8);JTextField t4=new JTextField(8);
        int str=0;
        int str1=0;
        p1(){
            super(new GridLayout(5,2,5,5));
            b1.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    count=Integer.valueOf(t1.getText());
                    cc=Integer.valueOf(t2.getText());
                    max=Integer.valueOf(t3.getText());
                    xz=Integer.valueOf(t4.getText());
                    star = System.currentTimeMillis()/1000;
                    MainJFrame Frame=new MainJFrame(count,cc,xz,max,star);
                }
            });
            add(l1);add(t1);add(l2);add(t2);add(l3);add(t3);add(l4);add(t4);add(l5);add(b1);
        }
        public int getcount()
        {
            return count;
        }
        public int getcc()
        {
            return cc;
        }
        public int getmax()
        {
            return max;
        }
        public int getxz()
        {
            return xz;
        }
        public long getstar()
        {
            return star;
        }
    }






public class Zhengshu {
    int zs1,zs2,sum=1;int fh;
    Zhengshu(){
        
    }
    Zhengshu(int Zs1,int Fh,int Zs2){
        zs1=Zs1;
        fh=Fh;
        zs2=Zs2;
    }
    public void setJiaf(int Zs1,int Zs2){
        sum=Zs1+Zs2;
    }
    public void setJianf(int Zs1,int Zs2){
        sum=Zs1-Zs2;
    }
    public  void setChengf(int Zs1,int Zs2){
        sum=Zs1*Zs2;
    }
    public void setChuf(int Zs1,int Zs2){
        sum=Zs1/Zs2;
    }
    public String getJiaf(){
        return zs1+"+"+zs2;
    }
    public String getJianf(){
        return zs1+"-"+zs2;
    }
    public String getChengf(){
        return zs1+"*"+zs2;
    }
    public String getChuf(){
        return zs1+"/"+zs2;
    }
    public int getSum(){
        return sum;
    }
    public String getsum(int zs1,int zs2){
        return zs1+"/"+zs2;
    }
}







public class Jiecheng {
    private int jc1;
    private int sum=1;
    private int n;
    Jiecheng(){
        
    }
    Jiecheng(int jc){
        jc1=jc;
        
    }
    public int getJieguo()
    {
        if(jc1==0)
            sum=1;
        else{
            for(n=1;n<=jc1;n++)
            {
                sum=sum*n;
            }
        }
        return sum;
    }
    public int getsum()
    {
        return sum;
    }
}





import java.util.Random;


public class Fenshu {
    private int fz,fm;//用户输入分子、分母
    private int fz0,fm0;//已约分正确分子、分母
    private int fz1=1,fm1=1;//未约分正确分子、分母
    private int jlfz,jlfm;//操作数
    private int temp,ys;//操作数
    private char ch;
    private char fh[]={'+','-','*','/'};
    Fenshu(){}
    public String suanshi(int a,int b,int c,int d,int z){
        if(a>b)//确保分母大于分子
        {
            temp=b;
            b=a;
            a=temp;
        }
        if(c>d)//确保分母大于分子
        {
            temp=d;
            d=c;
            c=temp;
        }
//        z=new Random().nextInt(cc*2-1);
        return a+"/"+b+" "+fh[z]+" "+c+"/"+d;
        
    }
    public String answer(int a,int b,int c,int d,int z){
        int shu []=new int [4];
        shu[0]=a;
        shu[1]=b;
        shu[2]=c;
        shu[3]=d;
        switch(z)
        {
        case 0:
            fz1=shu[0]*shu[3]+shu[1]*shu[2];
            fm1=shu[1]*shu[3];
            break;
        case 1:
            fz1=shu[0]*shu[3]-shu[1]*shu[2];
            fm1=shu[1]*shu[3];
            break;
        case 2:
            fz1=shu[0]*shu[2];
            fm1=shu[1]*shu[3];
            break;
        case 3:
            fz1=shu[0]*shu[3];
            fm1=shu[1]*shu[2];
            break;
        default:
            break;
        }
        jlfz=fz1;//约分
        jlfm=fm1;
        while(fm1!=0)
        {
            ys=fz1%fm1;
            fz1=fm1;
            fm1=ys;
        }
        fz0=jlfz/fz1;
        fm0=jlfm/fz1;
        if(fm0<0)
        {
            fz0=fz0*-1;
            fm0=fm0*-1;
        }
        return fz0+"/"+fm0;
    }

}

开发环境:Eclipse

做的是3.4.6.8,/*8没成功*/

结对同伴的学号:201306114327  姓名:林海信    博客地址:http://www.cnblogs.com/linhaixin/ 

这个结对编程过程中,我和同伴轮流编程,轮流检测,轮流查阅资料。

总结:本次编程预想时间1天,实际时间3天外加熬夜修改BUFF。在次过程中,体会到结对子的乐趣与作用,还可以从中相互学习。不过,这还是一个需要磨合的时间,否则会什么都做不成。我觉得,结对子学习非常的有效。

 

posted on 2015-04-09 18:22  35何武鹏  阅读(298)  评论(0编辑  收藏  举报