1.题目要求:能自动生成小学四则运算题目。
2.软件功能:
- (1)由计算机从题库文件中随机选择10道(数字在10以内)加减乘除混合算式,用户输入算式答案,程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;
- (2)增添了登录页面和保存文件功能,保存你的做题记录;
- (3)全部题答完可选择显示题目答案及分数;
- (4)暂时没有出带括号题目的功能。
3.代码说明:
- 登录界面
package Yang; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Login extends JFrame implements ActionListener { JButton jb1,jb2=null; JPanel jp1,jp2,jp3,jp4=null; JTextField jtf=null; JLabel jlb1,jlb2,jlb3=null; JPasswordField jpf=null; ButtonGroup bg=null; final String stu_name="ysh"; final String stu_pwd="123456"; public static void main(String[] args) { Login ms=new Login(); } public Login() { jb1=new JButton("登录"); jb2=new JButton("重置"); jb1.addActionListener(this); jb2.addActionListener(this); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); jlb1=new JLabel("用户名:"); jlb2=new JLabel("密 码:"); jtf=new JTextField(10); jpf=new JPasswordField(10); jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf); jp3.add(jb1); jp3.add(jb2); this.add(jp1); this.add(jp2); this.add(jp3); this.setLayout(new GridLayout(3,1)); this.setTitle("四则运算出题系统"); this.setSize(300,200); this.setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(true); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="登录"){ stulogin(); } else if(e.getActionCommand()=="重置"){ clear(); } } public void stulogin() { if(stu_name.equals(jtf.getText())&&stu_pwd.equals(jpf.getText())) { JOptionPane.showMessageDialog(null,"登录成功!\n"+"欢迎使用四则运算出题系统!",stu_name, JOptionPane.WARNING_MESSAGE); new MyExGUI(); clear(); setVisible(false); }else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jtf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE); }else if(jpf.getText().isEmpty()) { JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE); }else { JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE); clear(); } } public void clear() { jtf.setText(""); jpf.setText(""); } }
- 练习界面
package Yang; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import javax.swing.*; public class MyExGUI extends JFrame{ private ArrayList<String> user_zongti = new ArrayList<String>(); private ArrayList<String> user_zonganswer = new ArrayList<String>(); private ArrayList<String> user_answer = new ArrayList<String>(); private ArrayList<String> true_answer = new ArrayList<String>(); private ArrayList<String> jta_timu = new ArrayList<String>(); private ArrayList<String> jta_zong = new ArrayList<String>(); private ArrayList<Integer> user_fenshu = new ArrayList<Integer>(); JMenuBar jmb; //菜单条组件 JMenu menu1, menu2, menu3, menu4, menu5;//菜单 JMenuItem item1, item2, item3, item4, item5, item6;//菜单项 JMenu build; //二级菜单 JMenuItem file, project; TextArea answer_all = new TextArea(); TextField jta = new TextField(); TextField jta_answer = new TextField(); JLabel num_answer = new JLabel(); JLabel answer1; JToolBar jtb;//工具条 JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7,jb_next; int answer_count; int answer_fenshu; public MyExGUI() { //创建菜单 jmb = new JMenuBar(); menu1 = new JMenu("文件(F)"); menu1.setMnemonic('f'); //助记符 menu2 = new JMenu("编辑"); menu2.setMnemonic('E'); menu3 = new JMenu("格式"); menu4 = new JMenu("查看"); menu5 = new JMenu("帮助"); build = new JMenu("新建"); file = new JMenuItem("文件"); project = new JMenuItem("答题"); item1 = new JMenuItem("打开"); item2 = new JMenuItem("保存(S)"); item3 = new JMenuItem("另存为"); item4 = new JMenuItem("退出"); answer1 = new JLabel("第 1 题"); //添加菜单项至菜单上 build.add(file); build.add(project); menu1.add(build); menu1.add(item1); menu1.add(item2); menu1.add(item3); menu1.addSeparator(); menu1.add(item4); //将菜单加入至菜单栏 jmb.add(menu1); jmb.add(menu2); jmb.add(menu3); jmb.add(menu4); jmb.add(menu5); JPanel contentPanel = new JPanel(); contentPanel.setLayout(null); JLabel daan = new JLabel("答案"); JLabel dengyu = new JLabel("="); num_answer=answer1; num_answer.setFont(new Font("宋体",Font.BOLD, 22)); jb_next = new JButton("下一题"); jta.setFont(new Font("宋体",Font.BOLD, 22)); jta_answer.setFont(new Font("宋体",Font.BOLD, 22)); jb_next.setFont(new Font("宋体",Font.BOLD, 22)); daan.setFont(new Font("宋体",Font.BOLD, 22)); dengyu.setFont(new Font("宋体",Font.BOLD, 22)); contentPanel.add(num_answer); contentPanel.add(daan); contentPanel.add(dengyu); contentPanel.add(jta); contentPanel.add(jta_answer); contentPanel.add(answer_all); contentPanel.add(jb_next); num_answer.setBounds(90, 20, 130, 50); daan.setBounds(250, 20, 90, 50); jta.setBounds(50, 70, 150, 30); dengyu.setBounds(205, 70, 20, 20); jta_answer.setBounds(230, 70, 100, 30); jb_next.setBounds(350, 70, 110, 30); answer_all.setBounds(50, 120, 400, 300); this.setJMenuBar(jmb); //添加菜单栏,不能设定位置,会自动放在最上部 this.add(contentPanel); this.setTitle("小学生四则运算答题系统"); this.setSize(600, 500); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); item1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub JFileChooser jfc=new JFileChooser(); jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); jfc.showDialog(new JLabel(), "选择"); File file=jfc.getSelectedFile(); if(file.isDirectory()){ // System.out.println("文件夹:"+file.getAbsolutePath()); }else if(file.isFile()){ String s=file.getAbsolutePath(); } } }); item2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { FileOutputStream outSTr = null; BufferedOutputStream Buff = null; boolean flag = true; File file; //String test ; do{ //test = "test"+count; String inputValue = JOptionPane.showInputDialog("Please input file name"); file = new File(inputValue+".txt"); if (!file.exists()) { // 创建文件 try { flag=file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } flag=false; } else{ JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE); flag=true; } }while(flag); //写入文件 String u_answer; try { outSTr = new FileOutputStream(file); Buff = new BufferedOutputStream(outSTr); System.out.println("选择是后执行的代码"+user_zongti.size()+user_answer.size()); for (int i = 0; i < user_zongti.size(); i++) { try { Buff.write(user_zongti.get(i).getBytes()); Buff.write(" ".getBytes()); u_answer = user_answer.get(i); if(u_answer.equals("")) u_answer ="没有作答"; Buff.write(u_answer.getBytes()); Buff.write("\r\n".getBytes()); } catch (IOException e) { e.printStackTrace(); i--; } } Buff.flush(); Buff.close(); } catch (IOException e) { e.printStackTrace(); } //Buff.close(); try { outSTr.close(); } catch (IOException e) { e.printStackTrace(); } user_zongti.clear(); user_answer.clear(); } }); project.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { arithmetic art = new arithmetic(); true_answer=art.list_answer; jta_timu = art.list_timu; jta_zong = art.list; answer_count=1; answer_all.setText(""); for (int i = 0; i < art.list_timu.size(); i++) { user_zongti.add(jta_zong.get(i)); answer_all.append(jta_timu.get(i)); answer_all.append("\r\n"); } num_answer.setText("第 "+answer_count+" 题"); jta.setText(jta_timu.get(answer_count-1)); answer_count++; } }); jb_next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { String temp; temp = jta_answer.getText(); if(jta.getText().equals("")) { JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE); return; } jta_answer.setText(""); if(answer_count<=10) { if(isInteger(temp)) { user_answer.add(temp); System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size()); num_answer.setText("第 "+answer_count+" 题"); jta.setText(jta_timu.get(answer_count-1)); answer_count++; } else{ JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE); } } else{ user_answer.add(temp); System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size()); answer_fenshu=0; for(int i=0;i<user_answer.size();i++) { if(user_answer.get(i).equals(true_answer.get(i))) answer_fenshu+=10; } user_fenshu.add(answer_fenshu); Object[] options = { "是", "取消" }; int res=JOptionPane.showOptionDialog(null, "点击以继续 查看成绩", "答题完毕", JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, null, options, options[0]); if(res==JOptionPane.YES_OPTION){ OutputScore op =new OutputScore(user_fenshu,true_answer); }else{ Object[] option = { "是", "取消" }; int res1=JOptionPane.showOptionDialog(null, "继续新一轮答题", "新一轮答题", JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION, null, option, option[0]); if(res1==JOptionPane.YES_OPTION){ arithmetic art = new arithmetic(); true_answer=art.list_answer; jta_timu = art.list; answer_count=1; answer_all.setText(""); jta_answer.setText(""); for (int i = 0; i < art.list_timu.size(); i++) { user_zongti.add(jta_timu.get(i)); answer_all.append(jta_timu.get(i)); answer_all.append("\r\n"); } num_answer.setText("第 "+answer_count+" 题"); jta.setText(jta_timu.get(answer_count-1)); answer_count++; }else{ } } } } }); item4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub closeFrame(); } }); } public static boolean isInteger(String str) { for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } private void closeFrame() { System.out.println("调用窗体关闭功能"); int result = JOptionPane.showConfirmDialog(null,"是否要退出?","退出确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(result == JOptionPane.YES_OPTION) this.dispose(); } }
- 答案及分数界面
package Yang; import java.util.ArrayList; import javax.swing.*; public class OutputScore extends JFrame { //输出分数 public OutputScore(ArrayList<Integer> scores,ArrayList<String> answers) { super(); this.setLayout(null); int score=0; int j; JLabel jlb[]=new JLabel[10]; for(int i=0;i<answers.size();i++) { jlb[i]= new JLabel(); j=i+1; jlb[i].setText("第"+j+"题答案:"+answers.get(i)); jlb[i].setBounds(100,50+i*25,100,20); this.add(jlb[i]); } for(int i=0;i<scores.size();i++) { score=+scores.get(i); } JLabel lb=new JLabel(); lb.setText("你的分数为:"+score); this.setSize(300,400); this.add(lb); lb.setBounds(100,300,150,20); this.setVisible(true); } }
- 随机生成四则运算式
package Yang; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Random; public class arithmetic { ArrayList<String> list = new ArrayList<String>(); ArrayList<String> list_timu = new ArrayList<String>(); ArrayList<String> list_answer = new ArrayList<String>(); public arithmetic() { FileOutputStream outSTr = null; BufferedOutputStream Buff = null; int number_n=10,count; ArrayList<String> list_temp = new ArrayList<String>(); String[] operator = new String[]{"+","-","*","/"}; Random rand = new Random(); File file1 = new File("result.txt"); if (file1.exists()) { // 创建文件 try { file1.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } while(number_n>0) { int[] number_temp = new int[rand.nextInt(2)+3]; String[] str_temp = new String[number_temp.length-1]; for(int i=0;i<number_temp.length;i++) { if(i<number_temp.length-1) { number_temp[i]=rand.nextInt(100); list_temp.add(String.valueOf(number_temp[i])); str_temp[i]=operator[rand.nextInt(4)]; list_temp.add(str_temp[i]); } else { number_temp[i]=rand.nextInt(100); list_temp.add(String.valueOf(number_temp[i])); } } count=calculate_RPN(produce_RPN(list_temp)); if(count !=-1) { list_timu.add(transform_string(list_temp)); list_answer.add(String.valueOf(count)); list_temp.add(" = "+count); list.add(transform_string(list_temp)); number_n--; list_temp.clear(); } else list_temp.clear(); System.out.println(number_n); } try { outSTr = new FileOutputStream(file1); Buff = new BufferedOutputStream(outSTr); try { Buff.write("ysh".getBytes()); Buff.write("\r\n".getBytes()); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < list.size(); i++) { try { Buff.write(list.get(i).getBytes()); Buff.write("\r\n".getBytes()); } catch (IOException e) { e.printStackTrace(); i--; } } Buff.flush(); Buff.close(); } catch (IOException e) { e.printStackTrace(); } //Buff.close(); try { outSTr.close(); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); System.out.println(); } System.out.print("计算完毕!"); } public static int calculate_RPN(ArrayList<String> list_temp) { int i=0,t; double a=0,b=0; String l_temp; Stack sk=new Stack(10); for(t=0;t<list_temp.size();t++) { l_temp = list_temp.get(i++); if(!isInteger(l_temp)) { b = sk.mypop(); a = sk.mypop(); switch(l_temp) { case "+":sk.mypush(a+b);break; case "-":sk.mypush(a-b);break; case "*":sk.mypush(a*b);break; case "/": if(b==0) return -1; sk.mypush(a/b);break; } System.out.println("st.mytop: "+sk.mypeek()); } else{ sk.mypush((double)Integer.parseInt(l_temp)); } } if(!sk.myisempty()) { a = sk.mypop(); b = a-(int)a; System.out.println("a: "+a); if(a>0 && b==0 ) { return (int)a; } else return -1; } else return -1; } public static ArrayList<String> produce_RPN(ArrayList<String> list_temp) { int t=0,i=0; String tmp; Tack mytack = new Tack(10); ArrayList<String> lt_temp = new ArrayList<String>(); while(true) { tmp = list_temp.get(i++); if(isInteger(tmp)) { lt_temp.add(tmp); } else{ if(mytack.myisempty()) { mytack.mypush(tmp); } else{ if(isCPriority(tmp, mytack.mypeek())) mytack.mypush(tmp); else{ lt_temp.add(mytack.mypop()); mytack.mypush(tmp); } } } if(i>=list_temp.size()) { while(!mytack.myisempty()) lt_temp.add(mytack.mypop()); System.out.println(transform_string(list_temp)); list_temp = lt_temp; System.out.println(list_temp); return list_temp; } } } public static boolean isInteger(String str) { for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } public static boolean isCPriority(String str,String s) { if((str+s).equals("*+") || (str+s).equals("*-") || (str+s).equals("/+") || (str+s).equals("/-")) return true; else return false; } public static String transform_string(ArrayList<String> list_temp) { String s=""; for(int i=0;i<list_temp.size();i++) { s+=list_temp.get(i); } return s; } static class Stack { int mytop; double stk[]; public Stack(int num) { mytop=-1; stk=new double[num]; } /*出栈*/ double mypop() { double peek=stk[mytop]; mytop--; return peek; } /*入栈*/ void mypush(double x) { mytop++; stk[mytop]=x; } /*判空*/ Boolean myisempty() { if(mytop==-1) return true; else return false; } /*取栈顶元素*/ double mypeek() { double peek=stk[mytop]; return peek; } /*栈大小*/ int mysize() { return mytop+1; } } static class Tack { int mytop; String tk[]; public Tack(int num) { mytop=-1; tk=new String[num]; } /*出栈*/ String mypop() { String peek=tk[mytop]; mytop--; return peek; } /*入栈*/ void mypush(String x) { mytop++; tk[mytop]=x; } /*判空*/ Boolean myisempty() { if(mytop==-1) return true; else return false; } /*取栈顶元素*/ String mypeek() { String peek=tk[mytop]; return peek; } /*栈大小*/ int mysize() { return mytop+1; } } }
- 主函数
package Yang; public class MyGui { public static void main(String[] args) { Login lg = new Login(); //new MyExGUI(); } }
4、PSP表格
| 预计耗时(分钟) | 实际耗时(分钟) | ||
| Planning | 计划 | 25 | 30 |
| Estimate | 估计这个任务需要多少时间 | 5 | 5 |
| Development | 开发 | 90 | 85 |
| Analysis | 需求分析 | 10 | 10 |
| Design Spec | 生成设计文档 | 0 | 0 |
| Design Review | 设计复审(和同事审核设计文档) | 0 | 0 |
| Coding Standerd | 代码规范(为目前的开发制定合适的规范) | 5 | 5 |
| Design | 具体设计 | 20 | 30 |
| Coding | 具体编码 | 120 | 110 |
| Code Review | 代码复审 | 10 | 10 |
| Text | 测试(自测,修改代码,提交修改) | 20 | 20 |
| Reporting | 报告 | 10 | 10 |
| Text Report | 测试报告 | 10 | 10 |
| Size Measurement | 计算工作量 | 5 | 5 |
| Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 20 |
20 |
5、结果展示
(1)登录界面

(2)练习页面及得分页面

(3)保存文件

浙公网安备 33010602011771号