用Java编写GUI界面,模拟抢红包游戏,在界面输入红包大小和抢红包人姓名(用逗号隔开),输出系统当前时间以及每个人抢的红包大小,要求每个人抢的红包最少不能少于0.01元,精确到小数点后两位
时间: 2024-04-04 19:35:47 浏览: 82
好的,这是一个用Java编写的GUI界面程序,可实现你所需要的功能。你可以根据需要进行修改和完善。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class RedEnvelopeGame extends JFrame implements ActionListener {
private JLabel label1, label2, label3;
private JTextField textField1, textField2;
private JTextArea textArea;
private JButton button;
public RedEnvelopeGame() {
setTitle("Red Envelope Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 2, 5, 5));
setSize(400, 300);
label1 = new JLabel("Red Packet Size:");
add(label1);
textField1 = new JTextField();
add(textField1);
label2 = new JLabel("Player Names:");
add(label2);
textField2 = new JTextField();
add(textField2);
button = new JButton("Start Game");
add(button);
button.addActionListener(this);
label3 = new JLabel("Red Packet Details:");
add(label3);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
String input1 = textField1.getText();
String input2 = textField2.getText();
if (input1.isEmpty() || input2.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter both red packet size and player names.");
return;
}
try {
double redPacketSize = Double.parseDouble(input1);
String[] playerNames = input2.split(",");
int numPlayers = playerNames.length;
if (numPlayers == 0) {
JOptionPane.showMessageDialog(this, "Please enter at least one player name.");
return;
}
if (redPacketSize <= 0) {
JOptionPane.showMessageDialog(this, "Red packet size should be greater than zero.");
return;
}
double totalAmount = round(redPacketSize, 2);
ArrayList<Double> amounts = new ArrayList<>(Collections.nCopies(numPlayers, 0.0));
Random random = new Random();
while (totalAmount > 0) {
int i = random.nextInt(numPlayers);
double amount = round(random.nextDouble() * totalAmount, 2);
amounts.set(i, round(amounts.get(i) + amount, 2));
totalAmount = round(totalAmount - amount, 2);
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String dateStr = now.format(formatter);
textArea.setText(dateStr + "\n");
for (int i = 0; i < numPlayers; i++) {
textArea.append(playerNames[i] + ": " + format(amounts.get(i)) + "\n");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Invalid input.");
}
}
}
private double round(double value, int decimalPlaces) {
double factor = Math.pow(10, decimalPlaces);
return Math.round(value * factor) / factor;
}
private String format(double value) {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(value);
}
public static void main(String[] args) {
new RedEnvelopeGame();
}
}
```
使用方法:
1. 运行程序。
2. 在界面上输入红包大小和抢红包人姓名,然后点击“Start Game”按钮。
3. 程序会随机分配每个人抢到的红包金额,并在界面上显示当前时间和每个人抢到的金额。
注意事项:
1. 输入的红包大小必须为正数。
2. 输入的抢红包人姓名必须用逗号隔开。
3. 每个人抢到的红包金额不能少于0.01元,精确到小数点后两位。
4. 程序仅供参考,如有需要,请根据实际需求进行修改和完善。
阅读全文