201571030305、201571030306 《小学生四则运算结对项目》
小学四则运算结对项目
github地址:https://github.com/Suadea/https-github.com-Suadea
一、需求分析
1、需求分析
(1)由计算机从题库文件中随机选择20道加减乘除混合算式,用户输入算式答案,程序检查答案是否正确,每道题正确计5分,错误不计
分,20道题测试结束后给出测试总分;
(2)题库文件可采用实验二的方式自动生成,也可以手工编辑生成,文本格式如下:
(3)程序为用户提供三种进阶四则运算练习功能选择:百以内整数算式(必做)、带括号算式、真分数算式练习;
(4)程序允许用户进行多轮测试,提供用户多轮测试分数柱状图,示例如下:
(5)程序记录用户答题结果,当程序退出再启动的时候,可为用户显示最后一次测试的结果,并询问用户可否进行新一轮的测试;
(6)测试有计时功能,测试时动态显示用户开始答题后的消耗时间。
(7)程序人机交互界面是GUI界面(WEB页面、APP页面都可),界面支持中文简体(必做)/中文繁体/英语,用户可以进行语种选择。
二 核心代码:
class Num{ //定义数字类 private int a; public Num() { this.a = (int)(Math.random()*10000); } public Num(int i) { this.a = (int)(Math.random()*4)+1; } public int get_num() { return this.a; } public int num_sum(Num B) { return this.a + B.a; } public int num_dec(Num B) { return this.a - B.a; } public int num_c(Num B) { return this.a * B.a; } public int num_chu(Num B) { return this.a / B.a; } public void flash() { this.a = (int)(Math.random()*100); } public void flash_t() { this.a = (int)(Math.random()*4)+1; } public boolean pd(int i) { if(this.a == i) return true; else return false; } }
public class jiedui { private static Num a = new Num(); //a,b private static Num b = new Num(); private static Num c = new Num(1); private static int q = 0; private static String p; private static JFrame frame = new JFrame("简单四则运算"); private static JLabel label1 = new JLabel("A"); private static JLabel label2 = new JLabel("*"); private static JLabel label3 = new JLabel("B"); private static JLabel label4 = new JLabel(" "+"="+" "); private static JLabel label5 = new JLabel("结果"); private static JButton button = new JButton("查看"); private static JButton bt1 = new JButton("下一题"); private static JButton bt2 = new JButton("退出"); private static JTextField text = new JTextField(5); private static JPanel pane = new JPanel(); public static void main(String[] args) { pane.setBorder(BorderFactory.createEmptyBorder(20,60,20,60)); pane.setLayout(new GridLayout(0,5)); //设置布局 pane.add(label1); pane.add(label2); pane.add(label3); pane.add(label4); pane.add(text); pane.add(label5); pane.add(button); pane.add(bt1); pane.add(bt2); if(c.pd(1)) { p = "+"; q = a.num_sum(b); } if(c.pd(2)) { p = "-"; q = a.num_dec(b); } if(c.pd(3)) { p = "*"; q = a.num_c(b); } if(c.pd(4)) { p = "/"; q = a.num_chu(b); } frame.getContentPane().add(pane,BorderLayout.CENTER); button.setMnemonic(KeyEvent.VK_I); button.addActionListener(new ActionListener(){ //鼠标单击事件 public void actionPerformed(ActionEvent e){ if(q == Integer.parseInt(text.getText().trim())) label5.setText("正确!"); else label5.setText("错误!"); } }); bt1.setMnemonic(KeyEvent.VK_H); bt1.addActionListener(new ActionListener(){ //鼠标单击事件 public void actionPerformed(ActionEvent e){ a.flash(); b.flash(); c.flash_t(); if(c.pd(1)) { p = "+"; q = a.num_sum(b); } if(c.pd(2)) { p = "-"; q = a.num_dec(b); } if(c.pd(3)) { p = "*"; q = a.num_c(b); } if(c.pd(4)) { p = "/"; q = a.num_chu(b); } label1.setText(" "+a.get_num()+" "); label2.setText(p); label3.setText(" "+b.get_num()+" "); text.setText(" "); label5.setText("结果"); } }); bt2.setMnemonic(KeyEvent.VK_T); bt2.addActionListener(new ActionListener(){ //鼠标单击事件 public void actionPerformed(ActionEvent e){ System.exit(0); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); label1.setText(" "+a.get_num()+" "); label2.setText(p); label3.setText(" "+b.get_num()+" "); frame.setVisible(true); }