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; Box box1, box2, box3, box4; String s = null, w = null, m = null; Container con; String fuhao = ""; Choice c2; int count = 0; double temp; JTextArea area; public sizeyunsuan() { f = new JFrame(); f.setTitle("欢迎进入王铭霞制作的四则运算测试"); f.setSize(300, 150); f.setLocation(100, 100); 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("确定"); cancel = new JButton("取消"); button1 = new JButton("随机数1"); button3 = new JButton("随机数2"); box2.add(button1); box2.add(button3); box2.add(sub); sub.addActionListener(this); box2.add(cancel); box3 = Box.createVerticalBox(); box3.add(Box.createVerticalStrut(20)); box3.add(box1); box3.add(Box.createVerticalStrut(20)); box3.add(box2); box3.add(Box.createVerticalStrut(10)); con.add(box3); 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); } }); f.setVisible(true); } public static void main(String arg[]) { sizeyunsuan w = new sizeyunsuan(); }
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, "计算正确"); return; } if (temp < result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } if (temp > result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } } if (fuhao.equals("-")) { result = s1 - s2; temp = Integer.parseInt(last.getText()); if (temp < result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } if (temp == result) { JOptionPane.showMessageDialog(null, "计算正确"); return; } if (temp > result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } } if (fuhao.equals("*")) { result = s1 * s2; temp = Integer.parseInt(last.getText()); if (temp == result) { JOptionPane.showMessageDialog(null, "计算正确"); return; } if (temp < result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } if (temp > result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } } if (fuhao.equals("/")) { result = s1 / s2; temp = Integer.parseInt(last.getText()); if (temp > result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } if (temp < result) { JOptionPane.showMessageDialog(null, "计算错误"); return; } if (temp == result) { JOptionPane.showMessageDialog(null, "计算正确"); return; } } } public void itemStateChanged(ItemEvent ie) { if (ie.getSource() == c2) { String str1 = c2.getSelectedItem(); fanhui(str1); } } public String fanhui(String str2) { return fuhao = str2; } }
时间: 2024-10-10 05:03:09