福利彩票中奖核心代码-玩了一上午都没有中奖
package com.exmaple.demo;
import java.util.Random;
import java.util.Scanner;
/**
* @author zly
* @version 1.0.0
* @ClassName TwoColorBall.java
* @Description TODO
* @createTime 2022年11月16日 09:41:00
*/
public class TwoColorBall {
public static void main(String[] args) {
//构建奖池
int[] winningPool = buildWinningPool();
System.out.println();
//获取用户输入的彩票
int[] userInputNum = getUserInputNum();
System.out.print("您的号码:");
for (int i = 0; i < userInputNum.length; i++) {
System.out.print(userInputNum[i] + " ");
}
System.out.println();
System.out.print("奖池:");
for (int i = 0; i < winningPool.length; i++) {
System.out.print(winningPool[i] + " ");
}
System.out.println();
//判断是否中奖
judeWin(winningPool, userInputNum);
}
public static void judeWin(int[] winningPool, int[] userInputNum) {
//红球计数器
int redNumberCount = 0;
//篮球计数器
int blueNumberCount = 0;
//判断红球中奖个数
for (int i = 0; i < userInputNum.length - 1; i++) {
int redNumber = userInputNum[i];
for (int j = 0; j < winningPool.length - 1; j++) {
if (redNumber == winningPool[j]) {
redNumberCount++;
}
}
}
if (userInputNum[userInputNum.length - 1] == winningPool[winningPool.length - 1]) {
blueNumberCount++;
}
System.out.println("红球中奖个数:" + redNumberCount);
System.out.println("篮球中奖个数:" + blueNumberCount);
}
public static int[] getUserInputNum() {
int[] userInputNum = new int[7];
//先输入六个红球
for (int i = 0; i < userInputNum.length - 1; ) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第" + (i + 1) + "红球");
int redBall = scanner.nextInt();
boolean flag = contains(userInputNum, redBall);
if (redBall < 1 || redBall > 33) {
System.out.println("超出范围号码,请输入1-33直接号码");
} else {
if (!flag) {
userInputNum[i] = redBall;
i++;
} else {
System.out.println("号码已经存在,请重新输入");
}
}
}
System.out.println("输入最后一个蓝色球,范围1-16");
while (true) {
Scanner scanner = new Scanner(System.in);
int blueBall = scanner.nextInt();
if (blueBall < 1 || blueBall > 16) {
System.out.println("超出范围号码,请输入1-16之间号码");
} else {
userInputNum[userInputNum.length - 1] = blueBall;
break;
}
}
return userInputNum;
}
//生成中奖号码
public static int[] buildWinningPool() {
int[] winningPool = new int[7];
//生成随机6个红球1-33
Random random = new Random();
for (int i = 0; i < winningPool.length - 1; ) {
//保证6个红球没有重复,判断
int redBall = random.nextInt(33) + 1;
boolean flag = contains(winningPool, redBall);
if (!flag) {
winningPool[i] = redBall;
i++;
}
}
//生成随机1-16的篮球
winningPool[winningPool.length - 1] = random.nextInt(16) + 1;
return winningPool;
}
public static boolean contains(int[] winningPool, int number) {
for (int i = 0; i < winningPool.length; i++) {
if (winningPool[i] == number) {
return true;
}
}
return false;
}
}
努力提高自己的技术,不忘初心