package com.yaqi.test;
import java.util.Random;
import java.util.Scanner;
public class Test11_2 {
public static void main(String[] args) {
//1.生成中奖号码
int[] arr = createNumber();//123456 7
System.out.println("*******************");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println("*******************");
//2.用户输入彩票号码(红球+篮球)//654321
int[] userInputArr = userInputNumber();
//3.判断用户的中奖情况
int redCount = 0;
int blueCount = 0;
//判断红色球
for (int i = 0; i < userInputArr.length- 1; i++) {
int redNumber = userInputArr[i];
for (int j = 0; j < arr.length-1; j++) {
if(redNumber == arr[j]){
redCount++;
//如果找到了,那么后面的数字就没有必要继续的比较了
//跳出内循环,继续判断下一个红色球号码是否存在
break;
}
}
}
//判断蓝色球
int blueNumber = userInputArr[userInputArr.length-1];
if(blueNumber == arr[arr.length-1]){
blueCount++;
}
//根据红球的个数以及蓝球的个数来判断中奖情况
if(redCount == 6 && blueCount == 1){
System.out.println("恭喜你,中奖1000万");
}else if(redCount == 6 && blueCount == 0){
System.out.println("恭喜你,中奖500万");
}else if(redCount == 5 && blueCount == 1){
System.out.println("恭喜你,中奖3000");
}else if((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)){
System.out.println("恭喜你,中奖200");
}else if((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)){
System.out.println("恭喜你,中奖10");
}else if((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1)|| (redCount == 0 && blueCount == 1)){
System.out.println("恭喜你,中奖5");
}else{
System.out.println("谢谢参与,谢谢惠顾");
}
}
public static int[] userInputNumber(){
//1.创建数组用于添加购买的彩票号码
//6个红球 1个蓝球 数组长度:7
int[] arr = new int[7];
//2.利用键盘输入
Scanner sc = new Scanner(System.in);
//让用户输入红球号码
for (int i = 0; i < 6; ) {
System.out.println("请输入第"+(i+1)+"个红球号码");
int redNumber = sc.nextInt();
//redNumber 在1-33里不重复
if(redNumber >= 1 && redNumber <=33){
boolean falg = contians(arr,redNumber);
if(!falg){
//不存在
//有效的直接添加到数组中
arr[i] = redNumber;
i++;
}else{
//存在
System.out.println("当前红色球号码已存在,请重新输入:");
}
}else{
System.out.println("当前红色球号码超出范围,请重新输入:");
}
}
//用户趣输入篮球号码
System.out.println("请输入蓝球号码:");
while(true){
int blueNumber = sc.nextInt();
if(blueNumber >= 1&& blueNumber <= 16){
arr[arr.length-1] = blueNumber;
break;
}else{
System.out.println("当前蓝球号码都超出范围");
}
}
return arr;
}
public static int[] createNumber() {
//1.创建数组用于添加中奖号码
//6个红球 1个蓝球 数组长度:7
int[] arr = new int[7];
//2.随机生成号码添加到数组中
//红球:不能重复的 1 2 3 4 5 6
//篮球:可以跟红色球重复 5
Random r = new Random();
for (int i = 0; i < 6; ) {
int redNumber = r.nextInt(33) + 1;
boolean falg = contians(arr, redNumber);
if (!falg) {
//把红球号码添加到数组中
arr[i] = redNumber;
i++;
}
}
//生成篮球号码并添加到数组当中
int blueNumber = r.nextInt(16)+1;
arr[arr.length-1] = blueNumber;
return arr;
}
//用于判断数字在数组中是否存在
public static boolean contians ( int[] arr, int number){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == number) {
return true;
}
}
return false;
}
}
黑马练习——双色球
最新推荐文章于 2024-11-04 23:24:03 发布