早教吧作业答案频道 -->其他-->
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 + " 答错了! >_
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点纸牌游戏求高手给加一个...的网友还看了以下:
根据要求回答问题"502x4,250x6,525x2"的乘积正好是三辆车的车牌号的最后四位数字,请 2020-04-07 …
全国100所名校单元测试卷高三英语卷十四的答案 2020-05-13 …
答题者请将题看完!1.有三张扑克牌,拍的数字各不相同,并且都在10以内.把三张牌洗好后,分别发给甲 2020-05-16 …
悬赏!!谁有四年级-语文-全国68所名牌小学小学语文阅读训练80篇的答案? 2020-05-17 …
准备一副扑克牌,去掉J、Q、K和大、小王.从剩下的牌中随意拿出四张放在桌子上,根据这四张牌上的数字 2020-05-22 …
文中( 5 )处正确的答案是( )。 A.1个令牌帧和1个数据帧B.2个令牌帧C.1个令牌帧D.2个 2020-05-26 …
三个质数的倒数和是二百三十一分之一百三十一,这三个质数分别是多少?×××抽了两张牌,牌上数字之和是 2020-06-13 …
2007英语专四的答案我想要2007年专四的答案.一定要标准的哈,错的不来 2020-06-14 …
有十张扑克牌',依次排在桌面上,请问怎样才能每隔两张牌,把一张牌,放在另一张牌上,把牌分成五对.高 2020-06-16 …
下面两组数是同学们玩24点扑克牌游戏中四张牌上的四个数字,请你选用十、-、×、÷、()组成等式.1 2020-06-22 …