数组随机取数(大乐透机选投注)

数组随机取数(大乐透机选投注)闲着没事写着完呢packagecom.yjl.daletou.util;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importja

闲着没事写着完呢

package com.yjl.daletou.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class SwingIndexExample {

	private static JTextField inputText, redText, blueText, kjText, tzText, resultsText;
	private static JTextArea outputText;
	public static void main(String[] args) {    
        // 创建 JFrame 实例
        JFrame frame = new JFrame("体彩大乐透");
        // Setting the width and height of frame
        frame.setSize(540, 600);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* 创建面板,这个类似于 HTML 的 div 标签
         * 我们可以创建多个面板并在 JFrame 中指定位置
         * 面板中我们可以添加文本字段,按钮及其他组件。
         */
        JPanel panel = new JPanel();    
        // 添加面板
        frame.add(panel);
        //调用用户定义的方法并添加组件到面板
        placeComponents(panel);
        // 设置界面可见
        frame.setVisible(true);
    }

    private static void placeComponents(final JPanel panel) {

        //布局部分我们这边不多做介绍。这边设置布局为 null
        panel.setLayout(null);
        //创建 JLabel
        JLabel headerLabel = new JLabel("体彩大乐透机选投注");
        headerLabel.setBounds(175,20,190,25);
        headerLabel.setFont(new Font("楷体",Font.BOLD,20));
        panel.add(headerLabel);
        
        JLabel inputLabel = new JLabel("机选注数:");
        inputLabel.setFont(new Font("楷体",Font.BOLD,16));
        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
         */
        inputLabel.setBounds(50,70,90,25);
        panel.add(inputLabel);

        /* 
         * 创建文本框用于输入机选注数。
         */
        inputText = new JTextField(20);
        inputText.setFont(new Font("楷体",Font.BOLD,16));
        inputText.setText("5");
        inputText.setBounds(130,70,40,25);
        panel.add(inputText);
        
        /*
         * 前区号码球数量
         */
        JLabel redLabel = new JLabel("前:");
        redLabel.setFont(new Font("楷体",Font.BOLD,16));
        redLabel.setBounds(180,70,30,25);
        panel.add(redLabel);
        
        redText = new JTextField(20);
        redText.setFont(new Font("楷体",Font.BOLD,16));
        redText.setBounds(210,70,20,25);
        redText.setText("5");
        panel.add(redText);
        
        /*
         * 后区号码球数量
         */
        JLabel blueLabel = new JLabel("后:");
        blueLabel.setFont(new Font("楷体",Font.BOLD,16));
        blueLabel.setBounds(230,70,30,25);
        panel.add(blueLabel);
        
        blueText = new JTextField(20);
        blueText.setFont(new Font("楷体",Font.BOLD,16));
        blueText.setBounds(260,70,20,25);
        blueText.setText("2");
        panel.add(blueText);

        //机选后注数显示文本域
        JLabel outputLabel = new JLabel("机选号码:");
        outputLabel.setFont(new Font("楷体",Font.BOLD,16));
        outputLabel.setBounds(50,100,90,25);
        panel.add(outputLabel);

        //注数显示文本域
        outputText = new JTextArea(10,20);
        outputText.setLineWrap(true);    //设置文本域中的文本为自动换行
        outputText.setForeground(Color.BLACK);    //设置组件的背景色
        outputText.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        JScrollPane jsp = new JScrollPane(outputText);    //将文本域放入滚动窗口
        
        //Dimension size=outputText.getPreferredSize();    //获得文本域的首选大小
        jsp.setBounds(130,100,330,200);
        panel.add(jsp);    //将JScrollPane添加到JPanel容器中
        
        //开始按钮
        final JButton actionButton = new JButton("机选");
        actionButton.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        actionButton.setBounds(290, 70, 80, 25);
        panel.add(actionButton);
        actionButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		if(inputText.getText().equals("")){
        			JOptionPane.showMessageDialog(panel, "请输入机选注数!", "提示",JOptionPane.WARNING_MESSAGE);
        			return;
        		}
        		if(redText.getText().equals("")){
        			JOptionPane.showMessageDialog(panel, "前区球数不可小于5!", "提示",JOptionPane.WARNING_MESSAGE);
        			return;
        		}
        		if(blueText.getText().equals("")){
        			JOptionPane.showMessageDialog(panel, "后区球数不可小于2!", "提示",JOptionPane.WARNING_MESSAGE);
        			return;
        		}
        		int n = Integer.parseInt(inputText.getText());
        		int r = Integer.parseInt(redText.getText());
        		int b = Integer.parseInt(blueText.getText());
        		if(n > 100){
        			JOptionPane.showMessageDialog(panel, "最大可机选100注!", "提示",JOptionPane.WARNING_MESSAGE);
        		}else if(n < 1){
        			JOptionPane.showMessageDialog(panel, "最少机选1注!", "提示",JOptionPane.WARNING_MESSAGE);
        		}else if(r < 5){
        			JOptionPane.showMessageDialog(panel, "前区球数不可小于5!", "提示",JOptionPane.WARNING_MESSAGE);
        		}else if(b < 2){
        			JOptionPane.showMessageDialog(panel, "后区球数不可小于2!", "提示",JOptionPane.WARNING_MESSAGE);
        		}else{
        			for (int i = 0; i < n; i++) {
            			daLeTou(r,b);
            		}
        		}
        	}
        });
        
        //重置按钮
        final JButton resetButton = new JButton("重置");
        resetButton.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        resetButton.setBounds(380, 70, 80, 25);
        panel.add(resetButton);
        resetButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		inputText.setText("");
        		outputText.setText("");
        	}
        });
        
        //********************************************************************************
        
        JLabel queryLabel = new JLabel("中奖查询");
        queryLabel.setFont(new Font("楷体",Font.BOLD,20));
        queryLabel.setBounds(225,310,90,25);
        panel.add(queryLabel);
        
        JLabel kjLabel = new JLabel("开奖号码:");
        kjLabel.setFont(new Font("楷体",Font.BOLD,16));
        kjLabel.setBounds(50,350,90,25);
        panel.add(kjLabel);
        
        kjText = new JTextField(20);
        kjText.setFont(new Font("楷体",Font.BOLD,16));
        kjText.setBounds(130,350,330,25);
        panel.add(kjText);
        
        JLabel sampleLabel = new JLabel("示例:01,02,03,04,05|01,02");//示例
        sampleLabel.setFont(new Font("楷体",Font.BOLD,14));
        sampleLabel.setBounds(130,370,250,20);
        panel.add(sampleLabel);
        
        JLabel tzLabel = new JLabel("投注号码:");
        tzLabel.setFont(new Font("楷体",Font.BOLD,16));
        tzLabel.setBounds(50,390,90,25);
        panel.add(tzLabel);
        
        tzText = new JTextField(20);
        tzText.setFont(new Font("楷体",Font.BOLD,16));
        tzText.setBounds(130,390,330,25);
        panel.add(tzText);
        
        //开奖重置按钮
        final JButton kjResetButton = new JButton("开奖重置");
        kjResetButton.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        kjResetButton.setBounds(110, 420, 100, 25);
        panel.add(kjResetButton);
        kjResetButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		kjText.setText("");
        	}
        });
        
        //投注重置按钮
        final JButton tzResetButton = new JButton("投注重置");
        tzResetButton.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        tzResetButton.setBounds(220, 420, 100, 25);
        panel.add(tzResetButton);
        tzResetButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		tzText.setText("");
        	}
        });
        
        //查询按钮
        final JButton queryButton = new JButton("查询");
        queryButton.setFont(new Font("楷体",Font.BOLD,14));    //修改字体样式
        queryButton.setBounds(330, 420, 100, 25);
        panel.add(queryButton);
        queryButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e) {
        		String kjBall = kjText.getText();
        		if(kjBall.equals("")){
        			JOptionPane.showMessageDialog(panel, "请输入开奖号码!", "提示",JOptionPane.WARNING_MESSAGE);
        			return;
        		}
        		kjBall.replaceAll(",", ",");
        		kjBall.replaceAll("|", "|");
        		String tzBall = tzText.getText();
        		if(tzBall.equals("")){
        			JOptionPane.showMessageDialog(panel, "请输入投注号码!", "提示",JOptionPane.WARNING_MESSAGE);
        			return;
        		}
        		tzBall.replaceAll(",", ",");
        		tzBall.replaceAll("|", "|");

        		queryCompare(kjBall, tzBall);
        	}
        });
        //结果
        JLabel resultsLabel = new JLabel("中奖结果:");
        resultsLabel.setFont(new Font("楷体",Font.BOLD,16));
        resultsLabel.setBounds(50,460,90,25);
        panel.add(resultsLabel);
        
        resultsText = new JTextField(20);
        resultsText.setFont(new Font("楷体",Font.BOLD,16));
        resultsText.setBounds(130,460,330,25);
        panel.add(resultsText);
        
        //命中规则
        String gz = "一等奖:5+2  " +
        		"二等奖:5+1  " +
        		"三等奖:5+0  " +
        		"四等奖:4+2  <br/>"+
        		"五等奖:4+1  " +
        		"六等奖:3+2  " +
        		"七等奖:4+0  <br/>" +
        		"八等奖:3+1,2+2  " +
        		"九等奖:3+0,2+1,1+2,0+2  ";
        JLabel gzLabel = new JLabel("");
        gzLabel.setFont(new Font("楷体",Font.BOLD,12));
        gzLabel.setText("<html>" + gz + "</html>");
        gzLabel.setBounds(110,490,320,50);
        panel.add(gzLabel);
    }
    /**
     * 大乐透业务
     * 摇球
     */
    public static String[] shake(String[] ball, int arrLength){
    	String random = "";
		String[] rb;//红蓝球
		rb = ball;
		//摇球50次
		for(int y = 0; y < 50; y++){
			//System.out.println("摇前"+Arrays.toString(rb));
			String[] rb1 = new String[arrLength];//临时红篮球
			for (int i = 0; i < rb1.length; i++) {
				int index = (int) (Math.random() * rb.length);
				random = rb[index];
				rb1[i] = random;
				rb[index] = rb[rb.length-1];
				//数组缩容
				rb = Arrays.copyOf(rb, rb.length-1);
			}
			rb = new String[arrLength];
			for (int i=0;i<rb1.length;i++) {
				rb[i] = rb1[i];
	        }
			//System.out.println("摇后"+Arrays.toString(rb));
		}
    	return rb;
    }
    /**
     * 大乐透业务
     * 机选号码
     * @param r
     * @param b
     */
    public static void daLeTou(int r, int b){
		String random = "";
		String[] red = {"01","02","03","04","05","06","07","08","09","10",
				        "11","12","13","14","15","16","17","18","19","20",
				        "21","22","23","24","25","26","27","28","29","30",
				        "31","32"};
		/*
		 * /摇红球(摇法一:放在这个位置摇50次后,连续的随机抓取红球数)
		 */
		//red = shake(red, 32);
		//System.out.println("摇红球完毕"+Arrays.toString(red));
		//System.out.println("================");
		String[] blue = {"01","02","03","04","05","06","07","08","09","10",
		                 "11","12"};
		/*
		 * /摇蓝球(摇法一:放在这个位置摇50次后,连续的随机抓取蓝球数)
		 */
		//blue = shake(blue, 12);
		//System.out.println("摇篮球完毕"+Arrays.toString(blue));
		//System.out.println("================");
		String[] red1 = new String[r];
		String[] blue1 = new String[b];
		for (int i = 0; i < r; i++) {
			/*
			 * /摇红球(摇法二:放在这个位置摇50次后,随机抓取红球一个,然后剩余红球再摇50次,抓取红球第二个,根据i值依次类推)
			 */
			red = shake(red, 32-i);
			int index = (int) (Math.random() * red.length);
			random = red[index];
			red1[i] = random;
			red[index] = red[red.length-1];
			//数组缩容
			red = Arrays.copyOf(red, red.length-1);
		}
		for (int k = 0; k < b; k++) {
			/*
			 * /摇蓝球(摇法二:放在这个位置摇50次后,随机抓取蓝球一个,然后剩余篮球再摇50次,抓取篮球第二个,根据k值依次类推)
			 */
			blue = shake(blue, 12-k);
			//System.out.println("抓前"+Arrays.toString(blue));
			int index = (int) (Math.random() * blue.length);
			random = blue[index];
			blue1[k] = random;
			blue[index] = blue[blue.length-1]; 
			//数组缩容
			blue = Arrays.copyOf(blue, blue.length-1);
			//System.out.println("抓后"+Arrays.toString(blue));
		}
		red1 = paiXu(red1);
		blue1 = paiXu(blue1);
		outputText.append(Arrays.toString(red1)+" + "+Arrays.toString(blue1)+"\r\n");
	}
	/**
	 * 机选号码球由小到大排序
	 * @param input
	 * @return
	 */
	public static String[] paiXu(String[] input){
        for (int i=0;i<input.length-1;i++){
            for (int j=0;j<input.length-i-1;j++) {
                if(isBiggerThan(input[j],input[j+1])){
                    String temp=input[j];
                    input[j]=input[j+1];
                    input[j+1]=temp;
                }
            }
        }
        return input;
	}
	
	private static boolean isBiggerThan(String first, String second){
        if(first==null||second==null||"".equals(first) || "".equals(second)){
            System.out.println("字符串不能为空!");
            return false;
        }        
        char[] arrayfirst=first.toCharArray();
        char[] arraysecond=second.toCharArray();        
        int minSize = Math.min(arrayfirst.length, arraysecond.length);        
        for (int i=0;i<minSize;i++) {
            if((int)arrayfirst[i]>(int)arraysecond[i]){
                return true;
            }else if((int)arrayfirst[i] < (int)arraysecond[i]){
                return false;
            }
        }
        if(arrayfirst.length>arraysecond.length){
            return true;
        }else {
            return false;
        }
    }
	
	/**
	 * 结果查询比较中奖球数
	 */
	public static void queryCompare(String kjBall, String tzBall){
		//开奖球
		String kaijianghao = kjBall;
		String[] kj = kaijianghao.split("\\|");
		String k_red = kj[0];
		String k_blue = kj[1];
		//投注球
		String touzhuhao = tzBall;
		String[] tz = touzhuhao.split("\\|");
		String t_red = tz[0];
		String t_blue = tz[1];
		
		String[] k_red1 = k_red.split("\\,");//前区开奖号码
		String[] t_red1 = t_red.split("\\,");//前区投注号码
		int r = 0;//前区中奖球数
		for(int i = 0; i < k_red1.length; i ++){
			for(int k = 0; k<t_red1.length; k ++){
				if(k_red1[i].equals(t_red1[k])){
					r ++;
				}
			}
		}
		String[] k_blue1 = k_blue.split("\\,");//后区开奖号码
		String[] t_blue1 = t_blue.split("\\,");//后区投注号码
		int b = 0;//后区中奖球数
		for(int i = 0; i < k_blue1.length; i ++){
			for(int k = 0; k<t_blue1.length; k ++){
				if(k_blue1[i].equals(t_blue1[k])){
					b ++;
				}
			}
		}
		resultsText.setText(r+"+"+b);
	}
}

 

今天的文章数组随机取数(大乐透机选投注)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/64176.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注