java:机器随机产生一注双色球彩票

import java.util.Random;//随机产生整数
import java.util.Arrays;
/**
*@author 作者 Your-Name:
*@version 创建时间:
*类说明:开发双色球随机生成一注彩票
*软件分析:1,双色球,6个红色,1个蓝色球
*       2,红球由01-33,蓝色球01-16
*       3,红色球不能重复,位置不限,从小到大的顺序
*/
public class SuangSeQiu {
	public static int isIn(int array[],int n)
	{
		int i;
		for(i = 0;i < array.length;i ++)
		{
			if(n == array[i])
			{
				return 0;
			}
		}
		return 1;
	}

	public static void main(String[] args) {
		Random rd = new Random();
		// TODO Auto-generated method stub
		//方法一:for循环的嵌套
		/*int i,j,k;
		Random rd = new Random();
		int x[] = new int[6];
		int a;
		int l;
		l = rd.nextInt(16) + 1;
		for(i = 0;i < x.length;i ++)
		{
			a = rd.nextInt(33) + 1;
			x[i] = a;
			for(j = 0;j < i;j ++)
			{
				if(x[i] == x[j])
				{
					a = rd.nextInt(33) + 1;
					x[i] = a;
				}
					
			}
			
			
		}*/
		//方法二:普通方法
		int x[] = new int[6];
		int i;
		int l,index = 0,num;
		l = rd.nextInt(16) + 1;//随机产生0到15的整数,+ 1,随机产生1到16的整数
		int con;
		while(x.length > index)
		{
			num = rd.nextInt(33) + 1;
			con = isIn(x,num);
			if(con == 1)
			{
				x[index] = num;
				index ++;
			}
			
		}
		
		//排序
		System.out.println("开奖号码:");
		Arrays.sort(x);//从小到大排列的方法
		System.out.print("红球:");
		for(i = 0;i < x.length;i ++)
		{
			System.out.printf("%02d\t",x[i]);//格式输出0表示指定空位填0
		}
		System.out.print("蓝球: ");
		System.out.printf("%02d\n",l);
		
		
		
		

	}

}

你可能感兴趣的:(Java)