//双色球程序
public class doubleball {
public static void main(String[] args) {
int [] ballnum = getballnum();
show(ballnum);
}
public static int[] getballnum()
{
int ballnum[] = {0,0,0,0,0,0,0}; //前六为红球,后一位为蓝球,初始化都为零
final int LEN = 6;
int redball;
//随机生成前六个红球
for(int i = 0; i < LEN; i++)
{
redball = new Random().nextInt(33) + 1; //产生随机数,值范围(1~34)
if(GetSameNum(ballnum,redball)) //产生的数有重复,重新产生一个新随机数
{
redball = new Random().nextInt(33) + 1;
}
ballnum[i] = redball;
}
int blueball = new Random().nextInt(33) + 1; //随机生成蓝球
ballnum[LEN] = blueball;
return ballnum;
}
//判断产生的随机数和前面产生的数是否有相同,有返回true,没有返回false
public static boolean GetSameNum(int[] ballnum, int redball)
{
for(int j:ballnum) //相当于for( int i = 0; i < ballnum.lenght; i++) j值为 ballnum[i]
{
if(j == 0) //当值为0时,说明没有相同的,返回false
return false;
if(j == redball)
return true; //当有相同的,返回true
}
return false; //没有相同的,返回false
}
public static void show(int[] ballnum)
{
//输出结果
for(int i = 0; i < ballnum.length; i++)
{
if(i == 0)
{
System.out.print(ballnum[i] < 10 ? "0"+ballnum[i] : ballnum[i]);
}
else if( i == ballnum.length-1)
{
System.out.print("+"+(ballnum[i] < 10 ? "0"+ballnum[i] : ballnum[i]));
}
else
{
System.out.print(" "+(ballnum[i] < 10 ? "0"+ballnum[i] : ballnum[i]));
}
}
}
}