码迷,mamicode.com
首页 > 其他好文 > 详细

作业5.1

时间:2015-04-30 17:58:21      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.awt.Font;
public class calculator {
    private JFrame frame;
    private JTextField []textField= new JTextField[5];
    JLabel []label = new JLabel[5];
   int []r1=new int[5];
    int []r2=new int[5];
    String str3="";
    int Sum;
    int n=1;
    int[]sum=new int[5];
    String []r3=new String[5];
    String []str=new String[5];

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    calculator window = new calculator();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public calculator() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setTitle("\u5C0F\u5B66\u56DB\u5219\u8FD0\u7B97");
        frame.setBounds(100, 100, 450, 362);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        
         label[0] = new JLabel();
         label[0].setFont(new Font("宋体", Font.PLAIN, 27));
        label[0].setBounds(10, 50, 99, 29);
        frame.getContentPane().add(label[0]);
        
        label[1] = new JLabel();
        label[1].setFont(new Font("宋体", Font.PLAIN, 27));
        label[1].setBounds(10, 89, 99, 29);
        frame.getContentPane().add(label[1]);
        
        label[2] = new JLabel();
        label[2].setFont(new Font("宋体", Font.PLAIN, 27));
        label[2].setBounds(10, 128, 99, 29);
        frame.getContentPane().add(label[2]);
        
        label[3] = new JLabel();
        label[3].setFont(new Font("宋体", Font.PLAIN, 27));
        label[3].setBounds(10, 167, 99, 29);
        frame.getContentPane().add(label[3]);
        
        label[4] = new JLabel();
        label[4].setFont(new Font("宋体", Font.PLAIN, 27));
        label[4].setBounds(10, 206, 99, 29);
        frame.getContentPane().add(label[4]);
        
        textField[0] = new JTextField();
        textField[0].setFont(new Font("宋体", Font.PLAIN, 27));
        textField[0].setBounds(128, 50, 66, 29);
        frame.getContentPane().add(textField[0]);
        textField[0].setColumns(10);
        
        textField[1] = new JTextField();
        textField[1].setFont(new Font("宋体", Font.PLAIN, 27));
        textField[1].setColumns(10);
        textField[1].setBounds(128, 93, 66, 29);
        frame.getContentPane().add(textField[1]);
        
        textField[2] = new JTextField();
        textField[2].setFont(new Font("宋体", Font.PLAIN, 27));
        textField[2].setColumns(10);
        textField[2].setBounds(128, 132, 66, 29);
        frame.getContentPane().add(textField[2]);
        
        textField[3] = new JTextField();
        textField[3].setFont(new Font("宋体", Font.PLAIN, 27));
        textField[3].setColumns(10);
        textField[3].setBounds(128, 171, 66, 29);
        frame.getContentPane().add(textField[3]);
        
        textField[4] = new JTextField();
        textField[4].setFont(new Font("宋体", Font.PLAIN, 27));
        textField[4].setColumns(10);
        textField[4].setBounds(128, 210, 66, 29);
        frame.getContentPane().add(textField[4]);
        
        JButton button = new JButton("\u51FA\u9898");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Stamp st=new Stamp();
                sum=st.output(r1, r2, r3);//我想测一测
                                                        //将初始化的数组r1和r2的{1.2.3.4.5}传入
                                                        //我的output方法里
                                                        //得出的结果会不会是:和传入的值一样
                for(int i=0;i<5;i++)
                {
                    textField[i].setText(null);
                    Sum=0;
                    if(!r3[i].equals("!"))
                        label[i].setText(r1[i]+r3[i]+r2[i]+"=");
                    else//if(r3[i].equals("!"))
                    {
                        label[i].setText(r2[i]+r3[i]+"=");}
                    }        
        
            }
        });
        button.setBounds(16, 273, 93, 23);
        frame.getContentPane().add(button);
    //    while(){
            
        
        JButton button_1 = new JButton("\u786E\u5B9A");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int q=1;int []a=new int[5];int l=0;
                for(int i=0;i<5;i++)
                {a[i]=-1;
                 str[i]=textField[i].getText();
                 char[] ch=str[i].toCharArray();
                 for(int j=str[i].length()-1;j>-1;j--)
                 {
                     if(ch[j]<‘0‘||ch[j]>‘9‘){
                         
                        a[l]=i;
                        l++;
                        q=0;
                        break;
                     }
                 }

                }if(a[1]!=-1){
                    JOptionPane w=new JOptionPane();
                    w.showMessageDialog(w,"输入有非法字符!", "温馨提示", 0);
                    for(int i=0;i<l;i++){
                    textField[a[i]].setText(null);
                    }
                }
                if(q==1){
                Calc Ca=new Calc();
                int k=Ca.cc(r1, r2, r3, sum, str);
                str3="答对"+k/20+"题"+"\n"+"共得"+k+"分";
                JOptionPane sm=new JOptionPane(); 
                sm.showMessageDialog(sm, str3, "温馨提示", 1);
                Jf w=new Jf();
                n=w.Jfw();
            }}
        });
        //frame.dispose();
        button_1.setBounds(130, 273, 93, 23);
        frame.getContentPane().add(button_1);
    
    }
        public void close(){
            frame.dispose();
        }

}

public class Calc {
    public int cc(int r1[], int r2[], String r3[],int sum[],String str[]){
        //Stamp st=new Stamp();
        //st.output(r1, r2, r3,sum);
        int fenshu=0;
        for(int i=0;i<5;i++)
        {int n=Integer.valueOf(str[i]);
        System.out.println(str[i]+"  "+sum[i]);
            if(n==sum[i])
            {
                fenshu+=20;
            }
            //System.out.println(sum[i]);
        }
        System.out.print(fenshu);
        return fenshu;
    }

}

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Jf extends JFrame{
    JButton b1 = new JButton("是");
    JButton b2 = new JButton("否");
    JLabel label = new JLabel("是否继续:(退出点击关闭)");
    int k=1;
    int n=1;
    public  int Jfw(){
        this.setTitle("select");
        this.setResizable(false);
        this.setBounds(500, 350, 300, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        FlowLayout flow=new FlowLayout();
        this.getContentPane().setLayout(flow);
        add(label);
        add(b1);//add(b2);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                n=1;
                close();
            }
        });
    /*    b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                n=0;
                close();
            }
        });*/
        return n;
    }
    public void close(){
        this.dispose();
    }
    
}

import java.util.Random;
public class Stamp {
    static Random r = new Random();
    int y;
    int []sum=new int[5];
    public int[] output(int r1[],int r2[],String r3[]){
        
         for(int i=0;i<5;i++){
             sum[i]=0;
             r1[i] = r.nextInt(100);
             r2[i] = r.nextInt(100);
             y = 42+r.nextInt(6);
             while(y==46)
                {
                 y = 42+r.nextInt(6);
                }
                if(y==44)
                {
                    r2[i] = 2+r.nextInt(8);
                }
                switch(y)
                {
                case 42:
                    r3[i]="*";
                    sum[i]=r1[i]*r2[i];
                    System.out.print(sum[i]+"="+r1[i]+"*"+r2[i]);
                    break;
                case 43:
                    r3[i]="+";
                    sum[i]=r1[i]+r2[i];
                    System.out.println(sum[i]+"="+r1[i]+"+"+r2[i]);
                    break;
                case 44:
                    r3[i]="!";
                    sum[i]=1;
                    for(int n=r2[i];n>1;n--)
                    {
                    sum[i]=sum[i]*n;
                    }
                    System.out.println(sum[i]+"="+r1[i]+"+"+r2[i]);
                    break;
                case 45:
                    r3[i]="-";
                    sum[i]=r1[i]-r2[i];
                    System.out.println(sum[i]+"="+r1[i]+"-"+r2[i]);
                    break;
                case 47:
                    r3[i]="/";
                    sum[i]=r1[i]/r2[i];
                    System.out.println(sum[i]+"="+r1[i]+"/"+r2[i]);
                    break;
                }
                     
         }
                 return sum;
    }
}

1.因为要求换组员,上次作业和27号同学一起完成,这次作业与37号施少涛同学一起完成。http://www.cnblogs.com/shishaotao/

2.黑盒测试:技术分享点击确定->技术分享点击确定->(将非法字符清空)技术分享输入->技术分享

白盒测试:技术分享

3.单元测试:我们以数组的形式,数组先获取随机数存储进去,然后以数组的形式输出。因此,单元测试对数组进行初始化。技术分享最终输入的结果与算的结果相符合技术分享

4.总结:

预计用时半天,实际用时一天。

本次结对子编程非常有趣,又想到了一个功能,结束考试后,是否要再一次出题。还学会了单元测试,黑、白盒子测试技术。虽然我们把成品做出来了,过程中也有一些小争执。但是,我们主要的还是要学会怎么更好的运用JAVA编程,所以我们还要继续加油!~

作业5.1

标签:

原文地址:http://www.cnblogs.com/hewupeng/p/4469325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!