最近学习Java语言,学完数组这块,碰着一个双色球的综合练习,感觉非常不错,可以用来巩固连贯之前所学,分享出来给大家试试。
玩法规则:
“双色球”每注投注号码由6个红色号码和1个蓝色球号码组成。红色球号码从1-33中选择,蓝色球号码从1-16中选择。球的数字匹配数量和颜色决定了是否中奖。
具体中奖规则:
需实现功能:
本次练习通过Random、Arrays类,for循环、if语句、switch分支语句可实现,适合初学者用于练习连贯所学知识,培养编程思维。
以下,是本练习实现方法:
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class RandomDemo {
public static void main(String[] args) {
int[] red = new int[6];
int[] blue = new int[1];
int[] userred = new int[6];
int[] userblue = new int[1];
int choice;
boolean a;
int rednum=0,bluenum=0,swit=0;
red = randomRed(red);
blue = randomBlue(blue);
do {
System.out.println("-----欢迎进入双色球机选系统(本期奖次500万)-----");
System.out.println("1.机选双色球"+"\t\t\t"+"2.自选双色球");
Scanner input = new Scanner(System.in);
choice = input.nextInt();
if (choice!=1 && choice!=2){
System.out.println("输入错误,请重新输入!");
a = true;
}else {
a = false;
}
} while (a);
if (choice == 1){
userred = randomRed(userred);
userblue = randomBlue(userblue);
System.out.println("-----------机选结果为----------");
System.out.println("机选红球:"+Arrays.toString(userred));
System.out.println("机选蓝球:"+Arrays.toString(userblue));
rednum = queryRed(red,userred,rednum);
bluenum = queryBlue(blue,userblue,bluenum);
swit = rednum+bluenum;
} else {
System.out.println("请输入您选择的红球号码(1-33):");
Scanner input = new Scanner(System.in);
for (int i=0;i<userred.length;i++){
System.out.println("请输入第"+(i+1)+"个球:");
userred[i] = input.nextInt();
}
System.out.println("请输入您选择的蓝球号码(1-16):");
userblue[0] = input.nextInt();
System.out.println("-----------您的选择为----------");
System.out.println("您选择的红球:"+Arrays.toString(userred));
System.out.println("您选择的蓝球:"+Arrays.toString(userblue));
rednum = queryRed(red,userred,rednum);
bluenum = queryBlue(blue,userblue,bluenum);
swit = rednum+bluenum;
}
System.out.println("-----------开奖结果为----------");
System.out.println("本期中奖红球号码:"+Arrays.toString(red));
System.out.println("本期中奖蓝球号码:"+Arrays.toString(blue));
System.out.println("红球猜中:"+rednum);
System.out.println("蓝球猜中:"+(int)(bluenum*0.1));
switch (swit){
case 16:
System.out.println("红球猜中6个,蓝球猜中1个,恭喜你,获得一等奖!");
case 6:
System.out.println("红球猜中6个,蓝球没有猜中,恭喜你,获得二等奖!");
case 15:
System.out.println("红球猜中5个,蓝球猜中1个,恭喜你,获得三等奖!");
case 5:
System.out.println("红球猜中5个,蓝球没有猜中,恭喜你,获得四等奖!");
case 14:
System.out.println("红球猜中4个,蓝球猜中1个,恭喜你,获得四等奖!");
case 4:
System.out.println("红球猜中4个,蓝球没有猜中,恭喜你,获得五等奖!");
case 13:
System.out.println("红球猜中3个,蓝球猜中1个,恭喜你,获得五等奖!");
case 12:
System.out.println("红球猜中2个,蓝球猜中1个,恭喜你,获得六等奖!");
case 11:
System.out.println("红球猜中2个,蓝球猜中1个,恭喜你,获得六等奖!");
case 10:
System.out.println("红球没有猜中,蓝球猜中1个,恭喜你,获得六等奖!");
default:
System.out.println("很遗憾您没有中奖");
}
}
//随机数填充红球号码
public static int[] randomRed(int[] arr){
Random ran = new Random();
for (int i=0; i<arr.length ;i++){
arr[i] = ran.nextInt(32)+1;
if (i>0){
for (int j=0;j<i;j++){
if (arr[j] == arr[i]){
--i;
}
}
}
}
return arr;
}
//随机数填充蓝球号码
public static int[] randomBlue(int[] arr){
Random ran = new Random();
arr[0] = ran.nextInt(15)+1;
return arr;
}
public static int queryRed(int[] arr ,int[] arr1 ,int rednum){
for (int i = 0 ; i<arr.length ; i++){
for (int j = 0; j<arr1.length ; j++){
if (arr[i] == arr1[j]){
rednum++;
}
}
}
return rednum;
}
public static int queryBlue(int[] arr ,int[] arr1 ,int bluenum){
if (arr[0] == arr1[0]){
bluenum += 10;
}else {
bluenum = 0;
}
return bluenum;
}
}
运行结果:
以上是本人学习至此,做的练习的代码,旨在纪录,欢迎指正。
加油,👍