用java开发的24程序

                         

 本程序是用java开发的算24程序,由程序模拟产生四张牌,由用户输入计算表达式,算出结果。程序已经调试通过,如下:

                            李万鸿  

 

 import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TwentyFourl extends JFrame implements ActionListener {
 JTextArea ans;
 JTextField[] t = new JTextField[4];
 JTextField th, res;
 JLabel re;
 JButton eq;
 JButton play, sa;
 JScrollPane scroll;

 public TwentyFourl() {
  super("twentyfour");
  setSize(900, 600);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JPanel pane = new JPanel();
  pane.setLayout(new GridLayout(5, 1, 5, 5));
  JPanel p1 = new JPanel();
  play = new JButton("发牌");
  play.addActionListener(this);
  JPanel p2 = new JPanel();
  p2.setLayout(new GridLayout(1, 4, 10, 10));
  for (int i = 0; i < 4; i++) {
   long a = Math.round(Math.random() * 13);
   if (a == 0)
    a = 1;
   t[i] = new JTextField("" + a, 3);
   t[i].setHorizontalAlignment(SwingConstants.CENTER);
   t[i].setEditable(false);
   p2.add(t[i]);
  }
  pane.add(p2);
  p1.setLayout(new FlowLayout(FlowLayout.LEFT));
  p1.add(play);
  pane.add(p1);
  JPanel p3 = new JPanel();
  p3.setLayout(new FlowLayout(FlowLayout.LEFT));
  JLabel lbe = new JLabel("input answer:");
  p3.add(lbe);

  th = new JTextField(8);
  p3.add(th);

  eq = new JButton("提交");
  eq.addActionListener(this);
  p3.add(eq);
  res = new JTextField(3);
  res.setEditable(false);
  p3.add(res);
  pane.add(p3);

  JPanel p4 = new JPanel();
  p4.setLayout(new FlowLayout(FlowLayout.LEFT));
  re = new JLabel("result:");
  p4.add(re);
  pane.add(p4);
  JPanel p5 = new JPanel();
  p5.setLayout(new FlowLayout(FlowLayout.LEFT));
  sa = new JButton("show all answers");
  sa.addActionListener(this);
  ans = new JTextArea(8, 30);
  ans.setEditable(false);
  scroll = new JScrollPane(ans,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  // p5.add(sa);
  // p5.add(scroll);
  // pane.add(p5);
  setContentPane(pane);
  setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {

  String s = "";
  Object src = e.getSource();
  if (src == eq) {
   s = th.getText();
   s = strip(s);
   long r = f(s);
   res.setText("" + r);
   if (r == 24)
    re.setText("ok");
   else
    re.setText("error!");

  } else if (src == play) {

   for (int i = 0; i < 4; i++) {
    long a = Math.round(Math.random() * 13);
    if (a == 0)
     a = 1;
    t[i].setText("" + a);
    t[i].setHorizontalAlignment(SwingConstants.CENTER);

   }

  }

 }

 public String strip(String sa) {
  StringBuilder so = new StringBuilder();
  for (int i = 0; i < sa.length(); i++) {
   if (sa.charAt(i) != ' ')
    so.append(sa.charAt(i));

  }
  return so.toString();

 }

 public long f(String s) {
  long f1 = 0;
  String sk = null, s2 = null;
  if (s != null) {
   if ((s.indexOf('(') >= 0) || (s.indexOf(')') > 0)) {
    sk = get0(s);
    System.out.println(" f sk= " + sk);

    if (yn(sk) == 1)
     f1 = ct(sk);
    else
     f1 = f(sk);
    s2 = get2(s, sk, f1);
    System.out.println(" f s2= " + s2);
    if (yn(s2) == 1)
     f1 = ct(s2);
    else
     f1 = f(s2);
    return f1;
   } else if ((s.indexOf('*') > 0) || (s.indexOf('/') > 0)) {
    if (s.indexOf('/') > 0)
     sk = get(s, '/');
    else
     sk = get(s, '*');
    f1 = ct(sk);
    s2 = get2(s, sk, f1);
    if (yn(s2) > 1) {
     System.out.println(" f s2= " + s2);
     f1 = f(s2);
     return f1;
    } else
     return ct(s2);

   } else if ((s.indexOf('+') > 0) || (s.indexOf('-') > 0)) {
    if (s.indexOf('-') > 0)
     sk = get(s, '-');
    else
     sk = get(s, '+');
    f1 = ct(sk);
    s2 = get2(s, sk, f1);
    if (yn(s2) > 1) {
     f1 = f(s2);
     return f1;
    }
    return ct(s2);
   }
  }
  return -1;

 }

 

待续!

 

你可能感兴趣的:(用java开发的24程序)