- 题目:生成小学四则运算题目的 “软件”。 让程序能接受用户输入答案,并判定对错。 最后给出总共 对/错 的数量。
-
需求分析
- 能自动生成加、减、乘、除四则运算式子,并在窗口中显现出来。
- 判断输入的答案是否正确,并给出正确答案。
- 能统计出一共做对和做错多少道题目。
- 设计思路
- 做一个便于操作的界面,其中有两个随机按钮,用于给出随机数,有一个确定按钮,用于判断正误,并给出正确答案,和统计对错的个数。有一个取消按钮,用于取消输入的答案。一个符号按钮,由于选择加、减、乘、除。
- 做题时,只需点击两个随机按钮,一个选择按钮,输入答案,按确定键即可。
- 代码与运行结果如下
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Random; import java.util.Scanner; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class sizeyunsuan implements ActionListener, ItemListener { JFrame f; JTextField first, second, third, last; JButton sub, cancel, button1, button2, button3, fourth, denyu; Box box1, box2, box3, box4; String s = null, w = null, m = null; Container con; String fuhao = ""; Choice c2; int count = 0 ,count1=0; double temp; JTextArea textShow; public sizeyunsuan() { f = new JFrame(); f.setTitle("欢迎进入王铭霞制作的四则运算测试"); f.setSize(370, 320); f.setLocation(200, 200); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); con = f.getContentPane(); box1 = Box.createHorizontalBox(); first = new JTextField(5); c2 = new Choice(); c2.addItemListener(this); c2.add(""); c2.add("+"); c2.add("-"); c2.add("*"); c2.add("/"); third = new JTextField(5); fourth = new JButton("="); last = new JTextField(7); box1.add(first); box1.add(c2); box1.add(third); box1.add(fourth); box1.add(last); box2 = Box.createHorizontalBox(); sub = new JButton("confirm"); cancel = new JButton("cancel"); button1 = new JButton("random1"); button3 = new JButton("ramdom2"); box2.add(button1); box2.add(Box.createHorizontalStrut(10)); box2.add(button3); box2.add(Box.createHorizontalStrut(10)); box2.add(sub); box2.add(Box.createHorizontalStrut(10)); box2.add(cancel); sub.addActionListener(this); box3=Box.createHorizontalBox(); textShow=new JTextArea (150,100); box3.add(textShow); box4 = Box.createVerticalBox(); box4.add(Box.createVerticalStrut(20)); box4.add(box1); box4.add(Box.createVerticalStrut(20)); box4.add(box3); box4.add(Box.createVerticalStrut(20)); box4.add(box2); box4.add(Box.createVerticalStrut(10)); con.add(box4); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { s = String.valueOf(Math.round((Math.random() * 100))); first.setText(s); } }); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { w = String.valueOf(Math.round((Math.random() * 100) + 1)); third.setText(w); } }); cancel.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { if(e.getSource() ==cancel ) last.setText(null); } }); f.setVisible(true); } public static void main(String arg[]) { sizeyunsuan w = new sizeyunsuan(); } @Override public void actionPerformed(ActionEvent e) { double s1 = Integer.parseInt(first.getText()); double s2 = Integer.parseInt(third.getText()); double result = 0; if (fuhao.equals("+")) { result = s1 + s2; temp = Integer.parseInt(last.getText()); if (temp == result) { JOptionPane.showMessageDialog(null, "congradulation"); count=count+1; } if (temp != result) { JOptionPane.showMessageDialog(null, "sorry"); count1=count1+1; } } else if (fuhao.equals("-")) { result = s1 - s2; temp = Integer.parseInt(last.getText()); if (temp != result) { JOptionPane.showMessageDialog(null, "sorry"); count1=count1+1; } if (temp == result) { JOptionPane.showMessageDialog(null, "congradulation"); count=count+1; } } else if (fuhao.equals("*")) { result = s1 * s2; temp = Integer.parseInt(last.getText()); if (temp == result) { JOptionPane.showMessageDialog(null, "congradulation"); count=count+1; } if (temp != result) { JOptionPane.showMessageDialog(null, "sorry"); count1=count1+1; } } else if (fuhao.equals("/")) { result = s1 / s2; temp = Integer.parseInt(last.getText()); if (temp != result) { JOptionPane.showMessageDialog(null, "sorry"); count1=count1+1; } if (temp == result) { JOptionPane.showMessageDialog(null, "congradulation"); count=count+1; } } textShow.append(s1+""+fuhao+""+s2+"="+result+"\n"); textShow.append("right"+count+"\n"); textShow.append("wrong"+count1+"\n"); } public void itemStateChanged(ItemEvent ie) { if (ie.getSource() == c2) { String str1 = c2.getSelectedItem(); fanhui(str1); } } public String fanhui(String str2) { return fuhao = str2; } }