Flask接口签名sign原理与实例代码浅析
292
2022-12-06
java实现双色球抽奖算法
本文实例为大家分享了java实现双色球抽奖的具体代码,供大家参考,具体内容如下
实现双色球先考虑整体思路:
1.随机生成7位数的数组为大奖号码(前6位红球每个数值取值范围为1~33,而且6个数值不能相同,第七位篮球取值范围为1~16)
2.判断购买号码为手动输入还是机选(如为手动输入,将用for循环一次从控制台输入一个依次数赋值到数组;如为机选,实现代码和 1. 中相同)
3.将大奖号码和购买号码的前6位红球分别实现升序排列
4.判断大奖号码和购买号码的号码匹配程度来确定得奖级别或者没有得奖,并将结果输出到控制台
实现代码具体如下:
//导包
import java.util.*;
class TestDouble01
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Random ran = new Random();
//随机生成大奖号码
int arr2[]=new int[7];
int shu =33;
for (int i=0;i { boolean flag2=true; while (flag2) { if(i==6){ shu = 16; } int b = ran.nextInt(shu)+1; int num2=0; for (int j=0;j { if (b==arr2[j]) { num2++; } } if (num2==0) { arr2[i]=b; flag2=false; } } } //判断购买号码的生成方式 boolean flag=true; int arr1[]=new int[7]; while(flag){ System.out.println("请选择机选还是手选:1、机选;2、手选;3、退出 (请输入对应序号)"); int xz=s.nextInt(); if (xz == 1) { int shu1 = 33; //随机生成购买号码 for (int i=0;i { boolean flag1=true; while (flag1) { if (i==6) { shu1=16; } int a = ran.nextInt(shu1)+1; int num1=0; for (int j=0;j { if (a==arr1[j]) { num1++; } } if (num1==0) { arr1[i]=a; flag1=false; } } http:// } flag=false; }else if (xz==2) { //手动输入 for (int i=0;i { System.out.println("请输入第"+(i+1)+"个数:"); arr1[i]=s.nextInt(); } s.close(); }else if (xz==3) { //退出 flag=false; }else{ System.out.println("请输入正确的序号"); } } //将随机的大奖数组前6位红球升序排列 for(int j=0;j<=arr1.length-2;j++){ for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
boolean flag2=true;
while (flag2)
{
if(i==6){
shu = 16;
}
int b = ran.nextInt(shu)+1;
int num2=0;
for (int j=0;j { if (b==arr2[j]) { num2++; } } if (num2==0) { arr2[i]=b; flag2=false; } } } //判断购买号码的生成方式 boolean flag=true; int arr1[]=new int[7]; while(flag){ System.out.println("请选择机选还是手选:1、机选;2、手选;3、退出 (请输入对应序号)"); int xz=s.nextInt(); if (xz == 1) { int shu1 = 33; //随机生成购买号码 for (int i=0;i { boolean flag1=true; while (flag1) { if (i==6) { shu1=16; } int a = ran.nextInt(shu1)+1; int num1=0; for (int j=0;j { if (a==arr1[j]) { num1++; } } if (num1==0) { arr1[i]=a; flag1=false; } } http:// } flag=false; }else if (xz==2) { //手动输入 for (int i=0;i { System.out.println("请输入第"+(i+1)+"个数:"); arr1[i]=s.nextInt(); } s.close(); }else if (xz==3) { //退出 flag=false; }else{ System.out.println("请输入正确的序号"); } } //将随机的大奖数组前6位红球升序排列 for(int j=0;j<=arr1.length-2;j++){ for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
if (b==arr2[j])
{
num2++;
}
}
if (num2==0)
{
arr2[i]=b;
flag2=false;
}
}
}
//判断购买号码的生成方式
boolean flag=true;
int arr1[]=new int[7];
while(flag){
System.out.println("请选择机选还是手选:1、机选;2、手选;3、退出 (请输入对应序号)");
int xz=s.nextInt();
if (xz == 1)
{
int shu1 = 33;
//随机生成购买号码
for (int i=0;i { boolean flag1=true; while (flag1) { if (i==6) { shu1=16; } int a = ran.nextInt(shu1)+1; int num1=0; for (int j=0;j { if (a==arr1[j]) { num1++; } } if (num1==0) { arr1[i]=a; flag1=false; } } http:// } flag=false; }else if (xz==2) { //手动输入 for (int i=0;i { System.out.println("请输入第"+(i+1)+"个数:"); arr1[i]=s.nextInt(); } s.close(); }else if (xz==3) { //退出 flag=false; }else{ System.out.println("请输入正确的序号"); } } //将随机的大奖数组前6位红球升序排列 for(int j=0;j<=arr1.length-2;j++){ for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
boolean flag1=true;
while (flag1)
{
if (i==6)
{
shu1=16;
}
int a = ran.nextInt(shu1)+1;
int num1=0;
for (int j=0;j { if (a==arr1[j]) { num1++; } } if (num1==0) { arr1[i]=a; flag1=false; } } http:// } flag=false; }else if (xz==2) { //手动输入 for (int i=0;i { System.out.println("请输入第"+(i+1)+"个数:"); arr1[i]=s.nextInt(); } s.close(); }else if (xz==3) { //退出 flag=false; }else{ System.out.println("请输入正确的序号"); } } //将随机的大奖数组前6位红球升序排列 for(int j=0;j<=arr1.length-2;j++){ for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
if (a==arr1[j])
{
num1++;
}
}
if (num1==0)
{
arr1[i]=a;
flag1=false;
}
}
http:// }
flag=false;
}else if (xz==2)
{
//手动输入
for (int i=0;i { System.out.println("请输入第"+(i+1)+"个数:"); arr1[i]=s.nextInt(); } s.close(); }else if (xz==3) { //退出 flag=false; }else{ System.out.println("请输入正确的序号"); } } //将随机的大奖数组前6位红球升序排列 for(int j=0;j<=arr1.length-2;j++){ for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
System.out.println("请输入第"+(i+1)+"个数:");
arr1[i]=s.nextInt();
}
s.close();
}else if (xz==3)
{
//退出
flag=false;
}else{
System.out.println("请输入正确的序号");
}
}
//将随机的大奖数组前6位红球升序排列
for(int j=0;j<=arr1.length-2;j++){
for (int i = 0;i { if (arr1[i]>arr1[i+1]) { arr1[i]=arr1[i]+arr1[i+1]; arr1[i+1]=arr1[i]-arr1[i+1]; arr1[i]=arr1[i]-arr1[i+1]; } } } //将随机的购买数组前6位红球升序排列 for(int j=0;j<=arr2.length-2;j++){ for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
if (arr1[i]>arr1[i+1])
{
arr1[i]=arr1[i]+arr1[i+1];
arr1[i+1]=arr1[i]-arr1[i+1];
arr1[i]=arr1[i]-arr1[i+1];
}
}
}
//将随机的购买数组前6位红球升序排列
for(int j=0;j<=arr2.length-2;j++){
for (int i = 0;i { if (arr2[i]>arr2[i+1]) { arr2[i]=arr2[i]+arr2[i+1]; arr2[i+1]=arr2[i]-arr2[i+1]; arr2[i]=arr2[i]-arr2[i+1]; } } } //输出打印大奖号码和购买号码 System.out.println("购买号码为:"+Arrays.toString(arr1)); System.out.println("大奖号码为:"+Arrays.toString(arr2)); //判断奖项 //查询两个数组中前6位数字相同的数量 int num = 0; for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
if (arr2[i]>arr2[i+1])
{
arr2[i]=arr2[i]+arr2[i+1];
arr2[i+1]=arr2[i]-arr2[i+1];
arr2[i]=arr2[i]-arr2[i+1];
}
}
}
//输出打印大奖号码和购买号码
System.out.println("购买号码为:"+Arrays.toString(arr1));
System.out.println("大奖号码为:"+Arrays.toString(arr2));
//判断奖项
//查询两个数组中前6位数字相同的数量
int num = 0;
for (int i=0;i { for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
for (int j=0;j { if (arr2[i]==arr1[j]) { num++; } ohVAzHP } } //由相同的数字数量判断奖项 System.out.println(num); System.out.println(arr1[6]==arr2[6]); if (arr1[6]==arr2[6]) { if (num==6){ System.out.println("恭喜您获得一等奖"); }else if (num==5){ System.out.println("恭喜您获得三等奖"); }else if (num==4){ System.out.println("恭喜您获得四等奖"); }else if (num==3){ System.out.println("恭喜您获得五等奖"); }else if (num==2 || num==1 || num==0){ System.out.println("恭喜您获得六等奖"); }else{ System.out.println("您未获奖,再接再厉"); } }else{ if (num==6){ System.out.println("恭喜您获得二等奖"); }else if (num==5){ System.out.println("恭喜您获得四等奖"); }else if (num==4){ System.out.println("恭喜您获得五等奖"); }else{ System.out.println("您未获奖,再接再厉"); } } } } 代码运行结果如下: 由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
{
if (arr2[i]==arr1[j])
{
num++;
}
ohVAzHP }
}
//由相同的数字数量判断奖项
System.out.println(num);
System.out.println(arr1[6]==arr2[6]);
if (arr1[6]==arr2[6])
{
if (num==6){
System.out.println("恭喜您获得一等奖");
}else if (num==5){
System.out.println("恭喜您获得三等奖");
}else if (num==4){
System.out.println("恭喜您获得四等奖");
}else if (num==3){
System.out.println("恭喜您获得五等奖");
}else if (num==2 || num==1 || num==0){
System.out.println("恭喜您获得六等奖");
}else{
System.out.println("您未获奖,再接再厉");
}
}else{
if (num==6){
System.out.println("恭喜您获得二等奖");
}else if (num==5){
System.out.println("恭喜您获得四等奖");
}else if (num==4){
System.out.println("恭喜您获得五等奖");
}else{
System.out.println("您未获奖,再接再厉");
}
}
}
}
代码运行结果如下:
由于获奖为概率问题,所以想获得到结果为大奖的几率很低,大多数为:“未获奖 ”属于正常现象。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~