标签:
package size; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.Color; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JScrollPane; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.*; public class vvvv extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; int x;//题目个数 int y=0; int n; int i=1; char[] op = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ }; int[] nov = new int[4]; int zf;//控制正负 String str = new String(); String str1 = new String(); int[] num1 = new int[5]; String s[] = new String[5]; int num2=0; int[] anser=new int[5];//用户的答案 float[] trueanser=new float[5];//正确答案 File file; int a = 0; int b = 0; // int c = 0; public JFrame vvvv; JTextArea ck; private JTextField xmt; private JButton yes; private JButton no; private JButton start; private JButton huan; private JPanel panel; private JScrollPane scrollPane; /** * Launch the application. */ public static void main(String[] args)// 主方法 { EventQueue.invokeLater(new Runnable() { public void run() { try { vvvv window = new vvvv(); window.vvvv.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public vvvv() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { vvvv = new JFrame(); vvvv.getContentPane().setEnabled(false); vvvv.getContentPane().setBackground(Color.GRAY); vvvv.setTitle("\u56DB\u5219\u8FD0\u7B97"); vvvv.setBounds(100, 100, 494, 270); vvvv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); vvvv.setResizable(false); vvvv.getContentPane() .setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); panel = new JPanel(); vvvv.getContentPane().add(panel); ck = new JTextArea(10, 40); // panel.add(ck); // ck.setEditable(false); scrollPane = new JScrollPane(ck); panel.add(scrollPane); xmt = new JTextField(); vvvv.getContentPane().add(xmt); xmt.addActionListener(this); xmt.setColumns(10); yes = new JButton("\u786E\u8BA4"); vvvv.getContentPane().add(yes); yes.addActionListener(this); no = new JButton("\u6E05\u7A7A"); no.addActionListener(this); vvvv.getContentPane().add(no); huan = new JButton("\u6362\u80A4"); huan.addActionListener(this); vvvv.getContentPane().add(huan); start = new JButton("开始"); start.addActionListener(this); vvvv.getContentPane().add(start); } public void actionPerformed(ActionEvent e) { if (e.getSource() == yes) { if(b==1) { if(xmt.getText().length()!=0) try { x=Integer.parseInt(xmt.getText()); b++; } catch(NumberFormatException x) { ck.setText("输入有误!"); } else ck.setText("输入有误!"); } if(b==2) { if(n!=0)anser[i-2]=Integer.parseInt(xmt.getText()); str1="第"+i+"条题目为:"; n = (int) (Math.random() * 4 + 2);//随机出运算符的数量 for (int j = 0; j < n; j++) { zf = (int) (Math.random() * 2); if (zf == 0)// 负 num1[j] = -(int) (Math.random() * 100);//产生随机数 else // 正 num1[j] = (int) (Math.random() * 100); } for (int k = 0; k < n - 1; k++) { nov[k]=(int)(Math.random()*4); if(nov[k]==3&&num1[k+1]==0) { do { num1[k + 1]=(int)(Math.random()*100); }while(num1[k + 1]==0); } } str=""; for(int j=0;j<n;j++) { if(num1[j]<0)str=str+"("+String.valueOf(num1[j])+")"; else str=str+String.valueOf(num1[j]); if(j==n-1) { try { trueanser[i-1]=opt(str); } catch (Exception e1) { System.out.println(e1); //e1.printStackTrace(); } str=str+"="; //str=str+String.valueOf(trueanser[i-1]); ck.setText(str1+str); break; } str=str+op[nov[j]]; } if(i==x+1) { b=3; anser[i-2]=Integer.parseInt(xmt.getText()); } else i++; } if(b==3) { for(int j=0;j<x;j++) { if(anser[j]==(int)trueanser[j])y++; } ck.setText("答题完成!答对"+y+"道!"); b=0; } } if (e.getSource() == no) { xmt.setText(null); } if (e.getSource() == huan) { if (a == 0) { vvvv.getContentPane().setBackground(Color.blue); a++; } else if (a == 1) { vvvv.getContentPane().setBackground(Color.yellow); a++; } else if (a == 2) { vvvv.getContentPane().setBackground(Color.gray); a = 0; } } if (e.getSource() == start) { ck.setText("欢迎使用四则运算练习系统\n请输入题目的数量(1-5)"); n=0; i=1; b=1; } } public static float opt(String s) throws Exception{ if(s == null || "".equals(s.trim())) { return 0f; } int a1=s.indexOf("+"); int a2=s.indexOf("-"); int a3=s.indexOf("*"); int a4=s.indexOf("/"); int a5=s.indexOf("("); if(a1==-1&&a2==-1&&a3==-1&&a4==-1) { if(s.trim()==null||"".equals(s.trim())) { throw new Exception("operate error"); } return Float.parseFloat(s.trim()); } if(a5!=-1) { int a6=s.indexOf(")"); if(a6==-1) { throw new Exception("括号不匹配"); } else { float f=opt(s.substring(a5+1,a6).trim()); s=s.replace(s.substring(a5,a6+1), String.valueOf(f)); return opt(s); } } if(a1!=-1) { return opt(s.substring(0,a1))+opt(s.substring(a1+1,s.length())); } if(a2!=-1) { return opt(s.substring(0,a2))-opt(s.substring(a2+1,s.length())); } if(a3!=-1) { return opt(s.substring(0,a3))*opt(s.substring(a3+1,s.length())); } if(a4!=-1) { return opt(s.substring(0,a4))/opt(s.substring(a4+1,s.length())); } return Integer.parseInt(s.trim()); } }
实验并未完成,主要问题出在最后计算答案的时候,近期调试完后便把完成版发上来,现在先发未完成版,我的总结到时候也跟完成版一起上交....
PS:会出现这种情况完全是我自己太懒没有抓紧做...结果就变成这样了...真是十分抱歉(;´Д`)
PS2:同行的结对人是05莫俊余
PSP:(?Д?≡?Д?)老师手下留情!
标签:
原文地址:http://www.cnblogs.com/SiPine/p/4410696.html