早教吧 育儿知识 作业答案 考试题库 百科 知识分享

24点纸牌游戏求高手给加一个含括号的四则运算算法急需可追加分数第一个回答是我的程序代码

题目详情
24点纸牌游戏 求高手给加一个含括号的四则运算算法
急需可追加分数 第一个回答是我的程序代码
▼优质解答
答案和解析
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class PointCardFrame extends JFrame {
public static final double EPISON = 1E-6;
private CenterPanel centerpanel = new CenterPanel();
private HeadPanel headpanel = new HeadPanel();
private TailPanel tailpanel = new TailPanel();
public PointCardFrame() {
HeadPanel headpanel = new HeadPanel();
TailPanel tailpanel = new TailPanel();
add(headpanel, BorderLayout.NORTH);
add(centerpanel, BorderLayout.CENTER);
add(tailpanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame frame = new PointCardFrame();
frame.setTitle("24纸牌");
frame.pack(); //根据窗口里面的布局及组件的preferedSize来确定frame的最佳大小
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class HeadPanel extends JPanel {
private JButton jbtRefresh = new JButton("发牌");
private JButton jbtSolution = new JButton("答案");
private JTextField textField = new JTextField(15);
private String[] operator = { "+", "-", "*", "/", "(", ")" };
private double[] number = new double[4];
private String[] exp = new String[4];
public HeadPanel() {
add(jbtSolution, BorderLayout.WEST);
add(textField, BorderLayout.CENTER);
add(jbtRefresh, BorderLayout.EAST);
jbtRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
centerpanel.removeAll();
centerpanel.Display();
centerpanel.updateUI();
}
});
jbtSolution.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
number[0] = (double) centerpanel.cardvaule[0];
number[1] = (double) centerpanel.cardvaule[1];
number[2] = (double) centerpanel.cardvaule[2];
number[3] = (double) centerpanel.cardvaule[3];
exp[0] = String.valueOf(centerpanel.cardvaule[0]);
exp[1] = String.valueOf(centerpanel.cardvaule[1]);
exp[2] = String.valueOf(centerpanel.cardvaule[2]);
exp[3] = String.valueOf(centerpanel.cardvaule[3]);
if (is24(4)) {
textField.setText(exp[0]);
} else
textField.setText("没有答案");
}
});
}
public boolean is24(int n) {
if (n == 1)
return (Math.abs(number[0] - 24) < EPISON);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) { // 进行组合
double a, b;
String expa, expb;
a = number[i];
b = number[j];
number[j] = number[n - 1];
expa = exp[i];
expb = exp[j];
exp[j] = exp[n - 1];
exp[i] = "(" + expa + "+" + expb + ")";
number[i] = a + b;
if (is24(n - 1))
return true;
exp[i] = "(" + expa + "-" + expb + ")";
number[i] = a - b;
if (is24(n - 1))
return true;
exp[i] = "(" + expb + "-" + expa + ")";
number[i] = b - a;
if (is24(n - 1))
return true;
exp[i] = "(" + expa + "*" + expb + ")";
number[i] = a * b;
if (is24(n - 1))
return true;
if (b != 0) {
exp[i] = "(" + expa + "/" + expb + ")";
number[i] = a / b;
if (is24(n - 1))
return true;
}
if (a != 0) {
exp[i] = "(" + expb + "/" + expa + ")";
number[i] = b / a;
if (is24(n - 1))
return true;
}
number[i] = a; // 恢复
number[j] = b;
exp[i] = expa;
exp[j] = expb;
}
}
return false;
}
}
public class CenterPanel extends JPanel {
private int[] cardvaule = new int[4];
private int[] usedcard = new int[4];
public CenterPanel() {
loop: for (int i = 0; i < 4; i++) {
Random random = new Random();
int radomnumber = random.nextInt(13) + 1;
for (int k = 0; k < i; k++) {
if (usedcard[k] == radomnumber) {
i--;
continue loop;
}
}
String string = String.valueOf(radomnumber);
add(new JLabel(new ImageIcon("D:\\card\\" + string + ".JPG")));
usedcard[i] = radomnumber;
if (radomnumber % 13 != 0)
cardvaule[i] = radomnumber % 13;
else
cardvaule[i] = 13;
}
}
public void Display() {
usedcard[0] = -1;
usedcard[1] = -1;
usedcard[2] = -1;
usedcard[3] = -1;
loop: for (int i = 0; i < 4; i++) {
Random random = new Random();
int radomnumber = random.nextInt(13) + 1;
for (int k = 0; k < i; k++) {
if (usedcard[k] == radomnumber) {
i--;
continue loop;
}
}
String string = String.valueOf(radomnumber);
add(new JLabel(new ImageIcon("D:\\card\\" + string + ".JPG")));
usedcard[i] = radomnumber;
if (radomnumber % 13 != 0)
cardvaule[i] = radomnumber % 13;
else
cardvaule[i] = 13;
}
}
}
public class TailPanel extends JPanel {
private JLabel message = new JLabel("请输入算式");
private JTextField textField = new JTextField("", 15);
private JButton jbtVerify = new JButton("计算");
private int result;
public TailPanel() {
add(message, BorderLayout.WEST);
add(textField, BorderLayout.CENTER);
add(jbtVerify, BorderLayout.EAST);
jbtVerify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String experssion = textField.getText();




if (result == 24)
JOptionPane.showMessageDialog(null, "结果是 "
+ result + " 答对了 ! ^_^");
else if (result == -1999)
JOptionPane.showMessageDialog(null, "错误的表达式");
else
JOptionPane.showMessageDialog(null, "结果是"
+ result + " 答错了! >_
看了 24点纸牌游戏求高手给加一个...的网友还看了以下: