早教吧作业答案频道 -->其他-->
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点纸牌游戏求高手给加一个...的网友还看了以下:
11.生产、生活中到处都有物理化学知识,下面关于电压力锅的说法不正确的是A.电压力锅是将电能转化为 2020-04-26 …
受三个共点力作用而平衡的质点加速度一定等于0,其中任意两个力的合力一定与第三个力等大反向,该物体要 2020-05-13 …
寒字不要下面的两点,加一个足在下面,如何打!五笔或拼音都OK! 2020-05-14 …
一只叫Diamond的宠物狗和主人游戏,宠物狗沿直线奔跑,依次经过A、B、C三个木桩,B为AC的中 2020-05-16 …
石油称为原油,没有固定的沸点.加热原油使其气化,再分别冷却,在不同温度下可分别得到汽油、柴油、沥青 2020-05-16 …
汽车司机的反应时间为0.7s 司机从A点开始经反应时间刹车停在距A点17米的B点 加速度为5m/s 2020-05-16 …
请问这个字怎么读怎么打出来,这个是繁体字的写法,也有可能是日文汉字.急 这个字左右结构 左上 是 2020-05-17 …
有一根均匀木棒,放在一水平桌面上,它的一端伸到水平桌面外,伸到桌面外的长为木棒全长的四分之一,在木 2020-05-17 …
谁能帮我搞定这个小学四年级的24点游戏啊?用加、减、乘、除(可加括号)把给出的数算成24.每个数必 2020-05-20 …
MUD是一种基于文本界面的角色扮演的游戏,参加游戏的人扮演一个自己设定的角色,与其他众多游戏者在一个 2020-05-31 …