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 + " 答错了! >_
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
热门考点
- 1.有甲乙两个仓库,甲仓库存粮是乙仓库的4倍,从甲仓库中运出28又4分之3吨,往乙仓库中运进4.25吨两个仓库的存粮吨数相等,甲乙两个仓库原来各存粮多少吨?
- 一个圆柱形油桶,高4dm,底面半径2dm,做一个这样的油桶,至少要多少平方分米的铁皮
- 英语书“would”这个单词,上面说词性是modal,
- 在图所示电路中,闭合开关S,移动滑动变阻器的滑片P,当电压表的示数为6V 时,电流表示数为0.5A; 电压表的示数为7.2V时,电流表的示数为0.3A.则电阻R0为_Ω,电源电压为_V.
- 一块三角形地,底是150米 ,高是500分米,这块地共收稻谷2812.5 .平均每公顷收多少稻谷?
- 长方形面积咋算?
- 3,-5,7,4,二十四点怎么玩?
- 30度所对应的边是斜边的一半,那么这个三角形是直角三角形吗?是的那怎么证?
- k是什么值时,方程组3x+2y=k=1,4x+3y=k-1的解都是负数?(写出过程,
- 1.want,do,play,you,to,games,some,me,with,this,afternoon.2.the,it,first,l,cooked,was,meal.