java用户图形界面24点游戏的代码
时间: 2024-06-08 20:09:13 浏览: 18
以下是一个简单的 Java GUI 24 点游戏代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwentyFourGame {
private JFrame frame;
private JPanel panel;
private JLabel label1, label2, label3, label4, resultLabel;
private JTextField textField1, textField2, textField3, textField4;
private JButton button;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
TwentyFourGame window = new TwentyFourGame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public TwentyFourGame() {
initialize();
}
private void initialize() {
frame = new JFrame("24点小游戏");
frame.setBounds(100, 100, 500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
label1 = new JLabel("数字1:");
label1.setBounds(50, 30, 100, 20);
panel.add(label1);
textField1 = new JTextField();
textField1.setBounds(150, 30, 100, 20);
panel.add(textField1);
label2 = new JLabel("数字2:");
label2.setBounds(50, 70, 100, 20);
panel.add(label2);
textField2 = new JTextField();
textField2.setBounds(150, 70, 100, 20);
panel.add(textField2);
label3 = new JLabel("数字3:");
label3.setBounds(50, 110, 100, 20);
panel.add(label3);
textField3 = new JTextField();
textField3.setBounds(150, 110, 100, 20);
panel.add(textField3);
label4 = new JLabel("数字4:");
label4.setBounds(50, 150, 100, 20);
panel.add(label4);
textField4 = new JTextField();
textField4.setBounds(150, 150, 100, 20);
panel.add(textField4);
resultLabel = new JLabel();
resultLabel.setBounds(150, 190, 200, 20);
panel.add(resultLabel);
button = new JButton("计算");
button.setBounds(280, 150, 80, 20);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int a = Integer.parseInt(textField1.getText());
int b = Integer.parseInt(textField2.getText());
int c = Integer.parseInt(textField3.getText());
int d = Integer.parseInt(textField4.getText());
if (calculate24(a, b, c, d)) {
resultLabel.setText("恭喜你,可以用加减乘除得到24!");
} else {
resultLabel.setText("很遗憾,这四个数无法得到24!");
}
} catch (NumberFormatException ex) {
resultLabel.setText("请输入整数!");
}
}
});
}
private boolean calculate24(int a, int b, int c, int d) {
int[] nums = new int[]{a, b, c, d};
return dfs(nums, 0, 24);
}
private boolean dfs(int[] nums, int cur, int target) {
if (cur == 4) {
return target == 0;
}
for (int i = cur; i < 4; i++) {
swap(nums, cur, i);
if (dfs(nums, cur + 1, target - nums[cur]) ||
dfs(nums, cur + 1, target + nums[cur]) ||
dfs(nums, cur + 1, target * nums[cur]) ||
(target % nums[cur] == 0 && dfs(nums, cur + 1, target / nums[cur]))) {
return true;
}
swap(nums, cur, i);
}
return false;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
这个程序包含一个窗口,用户可以输入四个数字,然后点击“计算”按钮,程序会判断这四个数字是否可以使用加减乘除得到 24。程序中使用了深度优先搜索算法来实现这个功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)