标签:
开发环境:eclipse
结对同伴 :37 施少涛
同伴博客:http://www.cnblogs.com/shishaotao/
预计用时:半天
实际用时:1天
新增功能:1.空值异常处理 2.做错题目可提示做错并给出正确答案 3.可继续做题
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[]{1,2,3,4,5}; int []r2=new int[]{1,2,3,4,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, 310, 362); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); label[0] = new JLabel("",label[0].RIGHT); label[0].setFont(new Font("宋体", Font.PLAIN, 27)); label[0].setBounds(10, 50, 159, 29); frame.getContentPane().add(label[0]); label[1] = new JLabel("",label[1].RIGHT); label[1].setFont(new Font("宋体", Font.PLAIN, 27)); label[1].setBounds(10, 89, 159, 29); frame.getContentPane().add(label[1]); label[2] = new JLabel("",label[2].RIGHT); label[2].setFont(new Font("宋体", Font.PLAIN, 27)); label[2].setBounds(10, 128, 159, 29); frame.getContentPane().add(label[2]); label[3] = new JLabel("",label[3].RIGHT); label[3].setFont(new Font("宋体", Font.PLAIN, 27)); label[3].setBounds(10, 167, 159, 29); frame.getContentPane().add(label[3]); label[4] = new JLabel("",label[4].RIGHT); label[4].setFont(new Font("宋体", Font.PLAIN, 27)); label[4].setBounds(10, 206, 159, 29); frame.getContentPane().add(label[4]); textField[0] = new JTextField(); textField[0].setFont(new Font("宋体", Font.PLAIN, 27)); textField[0].setBounds(188, 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(188, 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(188, 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(188, 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(188, 210, 66, 29); frame.getContentPane().add(textField[4]); Stamp st=new Stamp(); sum=st.output(r1, r2, r3); for(int i=0;i<5;i++) { textField[i].setText(null); Sum=0; if(!r3[i].equals("!")){ if(r2[i]<0) label[i].setText(r1[i]+r3[i]+"("+r2[i]+")"+"="); else label[i].setText(r1[i]+r3[i]+r2[i]+"=");} else { if(r2[i]<0) label[i].setText(r1[i]+r3[i]+"("+r2[i]+")"+"="); else label[i].setText(r2[i]+r3[i]+"=");} } JButton button = new JButton("\u6362\u9898"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0){ Stamp st=new Stamp(); sum=st.output(r1, r2, r3); for(int i=0;i<5;i++) { textField[i].setText(null); Sum=0; if(!r3[i].equals("!")){ if(r2[i]<0) label[i].setText(r1[i]+r3[i]+"("+r2[i]+")"+"="); else label[i].setText(r1[i]+r3[i]+r2[i]+"=");} else { if(r2[i]<0) label[i].setText(r1[i]+r3[i]+"("+r2[i]+")"+"="); else label[i].setText(r2[i]+r3[i]+"=");} } } }); button.setBounds(27, 273, 93, 23); frame.getContentPane().add(button); 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‘)&&ch[j]!=‘-‘){ a[l]=i; l++; q=0; break; } } }if(a[0]!=-1){ JOptionPane w=new JOptionPane(); w.showMessageDialog(w,"输入有非法字符!", "温馨提示", 0); for(int i=0;i<l;i++){ textField[a[i]].setText(null); } } if(q==1){ frame.dispose(); Output ou=new Output(); ou.main(sum,str); } } }); button_1.setBounds(170, 273, 93, 23); frame.getContentPane().add(button_1); } } import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.SwingConstants; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Output { private JFrame frame; JLabel []label=new JLabel[5]; static String []string=new String[5]; static String st=null; int a=0; /** * Launch the application. */ public static void main(int sum[],String str[]) { int fenshu=0; int s; try{ for(int i=0;i<5;i++) { int n=Integer.valueOf(str[i]); string[i]=null; s=i+1; if(n==sum[i]) { fenshu+=20; string[i]="第"+s+"答对了;"; } else{ string[i]="第"+s+"答错了;正确答案为"+sum[i]; } }} catch(NumberFormatException e){ JOptionPane w=new JOptionPane(); w.showMessageDialog(w,"答案不能为空!", "温馨提示", 0); } int x,y;x=fenshu/20;y=5-x; st="答对"+x+"题;答错"+y+"题;总得分:"+fenshu+"分"; EventQueue.invokeLater(new Runnable() { public void run() { try { Output window = new Output(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Output() { //super(); initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 381); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); label[0] = new JLabel("New label"); label[0].setFont(new Font("宋体", Font.PLAIN, 25)); label[0].setBounds(10, 29, 414, 37); frame.getContentPane().add(label[0]); label[1] = new JLabel("New label"); label[1].setFont(new Font("宋体", Font.PLAIN, 25)); label[1].setBounds(10, 76, 414, 37); frame.getContentPane().add(label[1]); label[2] = new JLabel("New label"); label[2].setFont(new Font("宋体", Font.PLAIN, 25)); label[2].setBounds(10, 123, 414, 37); frame.getContentPane().add(label[2]); label[3] = new JLabel("New label"); label[3].setFont(new Font("宋体", Font.PLAIN, 25)); label[3].setBounds(10, 170, 414, 37); frame.getContentPane().add(label[3]); label[4] = new JLabel("New label"); label[4].setFont(new Font("宋体", Font.PLAIN, 25)); label[4].setBounds(10, 217, 414, 37); frame.getContentPane().add(label[4]); JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25)); lblNewLabel.setBounds(10, 253, 414, 41); frame.getContentPane().add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("\u7B54\u9898\u60C5\u51B5\u603B\u7ED3"); lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20)); lblNewLabel_1.setBounds(10, 10, 414, 22); frame.getContentPane().add(lblNewLabel_1); for(int i=0;i<5;i++){ label[i].setText(string[i]); } lblNewLabel.setText(st); JButton btnNewButton = new JButton("\u5173\u95ED"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { frame.dispose(); } }); btnNewButton.setBounds(260, 304, 93, 28); frame.getContentPane().add(btnNewButton); JButton btnNewButton_1 = new JButton("\u7EE7\u7EED\u505A\u9898"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.dispose(); calculator w=new calculator(); w.main(null); } }); btnNewButton_1.setBounds(83, 304, 93, 28); frame.getContentPane().add(btnNewButton_1); } } 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] = -100+r.nextInt(200); r2[i] =-100+r.nextInt(200); y = 42+r.nextInt(6); if(y==47) if(r2[i]==0){ r2[i]=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; } } 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; } }
import static org.junit.Assert.*; import org.junit.Test; public class Wqtext { @Test public void testStamp() { int d[]={0,0,0,0,0}; int a[]={0,1,2,3,4}; int b[]={0,1,2,3,4}; String c[]={"+","+","+","+","+"}; Stamp stamp=new Stamp(); d=stamp.output(a, b, c); assertEquals(0,d[0]); // fail("尚未实现"); } }
感想:本次的作业,我的小伙伴表现的很好,由于时间的紧迫,我和他分工合作,不过,很多是由我的小伙伴完成的~本次结对子也学到了几个以前没有学过的知识,希望下次的合作同样愉快!
标签:
原文地址:http://www.cnblogs.com/hewupeng/p/4488234.html