这是一个使用 Java Swing 编写的彩票预测器程序,它可以模拟不同彩种的开奖结果,并根据用户输入的数字判断是否中奖。

程序流程:

  1. 创建主窗口和控件面板: 创建一个 JFrame 窗口作为主窗口,并创建 JPanel 来存放各个控件。
  2. 添加输入控件: 在控件面板中添加 JLabel、JTextField、JComboBox 和 JButton,分别用于显示提示信息、接收用户输入的数字、选择彩种和触发预测操作。
  3. 添加开奖历史面板: 创建另一个 JPanel 来存放开奖历史记录,使用 JTable 来展示历史数据,并使用 JScrollPane 来实现滚动功能。
  4. 添加输出面板: 创建一个 JPanel 来显示预测结果,使用 JLabel 来展示预测结果信息。
  5. 将控件添加到主窗口: 将所有 JPanel 添加到 JFrame 中,并设置布局。
  6. 添加按钮事件监听器: 为预测按钮添加 ActionListener,并在 actionPerformed 方法中获取用户输入的数字和彩种,调用 predict 方法进行预测。
  7. 预测方法: 根据用户选择的彩种,使用 Random 类生成随机的开奖号码,并将结果存储在 ArrayList 中。
  8. 判断中奖: 检查用户输入的数字是否包含在生成的开奖号码中,并根据结果在输出面板中显示相应的信息。
  9. 添加开奖历史: 将生成的开奖号码添加到开奖历史表格中。
  10. 显示欢迎信息: 在程序启动时,使用 JOptionPane 显示欢迎信息。

代码示例:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class LotteryPredictor implements ActionListener {
private JFrame mainFrame;
private JPanel controlPanel;
private JPanel lotteryPanel;
private JLabel numInputLabel;
private JTextField numInput;
private JComboBox<String> lotteryType;
private JButton predictBtn;
private JTable historyTable;
private DefaultTableModel historyModel;
private JPanel outputPanel;
private JLabel outputLabel;
private ArrayList<Integer> winningNumbers;

public LotteryPredictor() {
    prepareGUI();
}

public static void main(String[] args) {
    LotteryPredictor lotteryPredictor = new LotteryPredictor();
    lotteryPredictor.showEvent();
}

private void prepareGUI() {
    // 创建主窗口
    mainFrame = new JFrame('彩票预测系统');
    mainFrame.setSize(600, 400);
    mainFrame.setLayout(new BorderLayout());
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // 创建控件面板
    controlPanel = new JPanel(new GridLayout(2, 3));
    numInputLabel = new JLabel('输入数字(1 - 50): ');
    numInput = new JTextField(10);
    lotteryType = new JComboBox<String>(new String[] {'双色球', '大乐透', '七星彩'});
    predictBtn = new JButton('预测');
    controlPanel.add(numInputLabel);
    controlPanel.add(numInput);
    controlPanel.add(new JLabel('彩种: '));
    controlPanel.add(lotteryType);
    controlPanel.add(predictBtn);
    predictBtn.addActionListener(this);

    // 创建开奖历史面板
    lotteryPanel = new JPanel(new BorderLayout());
    historyModel = new DefaultTableModel(new String[] {'期号', '中奖号码'}, 0) {
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    historyTable = new JTable(historyModel);
    JScrollPane scrollPane = new JScrollPane(historyTable);
    lotteryPanel.add(new JLabel('开奖历史'), BorderLayout.NORTH);
    lotteryPanel.add(scrollPane, BorderLayout.CENTER);

    // 创建输出面板
    outputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    outputLabel = new JLabel();
    outputPanel.add(outputLabel);

    // 添加控件到主窗口
    mainFrame.add(controlPanel, BorderLayout.NORTH);
    mainFrame.add(lotteryPanel, BorderLayout.CENTER);
    mainFrame.add(outputPanel, BorderLayout.SOUTH);

    // 显示主窗口
    mainFrame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == predictBtn) {
        try {
            int num = Integer.parseInt(numInput.getText());
            if (num > 0 && num < 51) {
                predict(num, lotteryType.getSelectedItem().toString());
            } else {
                JOptionPane.showMessageDialog(mainFrame, '请输入1 - 50之间的数字!', '输入错误', JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(mainFrame, '请输入正确的数字!', '输入错误', JOptionPane.ERROR_MESSAGE);
        }
    }
}

private void predict(int num, String lottery) {
    winningNumbers = new ArrayList<Integer>();
    Random random = new Random();
    if (lottery.equals('双色球')) {
        while (winningNumbers.size() < 6) {
            int winningNum = random.nextInt(33) + 1;
            if (!winningNumbers.contains(winningNum)) {
                winningNumbers.add(winningNum);
            }
        }
        int redBallNum = random.nextInt(16) + 1;
        outputResult(num, '红球' + winningNumbers.toString() + ', 蓝球' + redBallNum, lottery);
    } else if (lottery.equals('大乐透')) {
        while (winningNumbers.size() < 5) {
            int winningNum = random.nextInt(35) + 1;
            if (!winningNumbers.contains(winningNum)) {
                winningNumbers.add(winningNum);
            }
        }
        ArrayList<Integer> blueBalls = new ArrayList<Integer>();
        while (blueBalls.size() < 2) {
            int blueBallNum = random.nextInt(12) + 1;
            if (!blueBalls.contains(blueBallNum)) {
                blueBalls.add(blueBallNum);
            }
        }
        outputResult(num, '前区' + winningNumbers.toString() + ', 后区' + blueBalls.toString(), lottery);
    } else if (lottery.equals('七星彩')) {
        while (winningNumbers.size() < 7) {
            int winningNum = random.nextInt(10);
            if (!winningNumbers.contains(winningNum)) {
                winningNumbers.add(winningNum);
            }
        }
        outputResult(num, winningNumbers.toString(), lottery);
    }
}

private void outputResult(int num, String winningNumbersInfo, String lottery) {
    if (winningNumbers.contains(num)) {
        outputLabel.setText('恭喜您!这期' + lottery + '的中奖号码为:' + winningNumbersInfo + ',您选中了其中的' + num + '号!');
    } else {
        outputLabel.setText('很遗憾!这期' + lottery + '的中奖号码为:' + winningNumbersInfo + ',您没能选中其中的' + num + '号!');
    }
    addHistory(lottery, winningNumbersInfo);
}

private void addHistory(String lottery, String winningNumbers) {
    int period = historyModel.getRowCount() + 1;
    Object[] row = {period, winningNumbers};
    historyModel.addRow(row);
}

private void showEvent() {
    // 显示欢迎信息
    JOptionPane.showMessageDialog(mainFrame, '欢迎使用彩票预测系统!', '欢迎', JOptionPane.INFORMATION_MESSAGE);
}
}

注意:

  • 这个程序只是模拟彩票开奖结果,并不能真正预测彩票号码。
  • 程序中的彩种和号码范围可以根据需要进行修改。
  • 可以进一步扩展程序,例如添加中奖等级判断、中奖金额计算等功能。
Java 彩票预测器:模拟彩票开奖结果,并根据用户输入判断是否中奖

原文地址: https://www.cveoy.top/t/topic/oAmE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录