主要流程如下:1. 使用WinningNumber()方法随机生成一组1-33的红球号码和1-16的蓝球号码,作为中奖号码。2. 同样使用WinningNumber()方法随机生成一组作为用户号码。3. 判断用户号码中红球和蓝球的中奖情况:
- 红球判断使用contains()方法检查用户号码的红球是否在中奖号码的红球中。
- 蓝球直接比较两组号码的第6位(蓝球号码)。4. 根据中奖情况判断中奖级别和奖金:
- 6红球1蓝球:一等奖6000万
- 6红球0蓝球:二等奖500万
- 5红球1蓝球或5红球0蓝球:三等奖3000元
- 4红球1蓝球或4红球0蓝球:四等奖200元
- 3红球1蓝球或2红球1蓝球:五等奖10元
- 1红球1蓝球或0红球1蓝球:六等奖5元
- 否则:与奖擦肩而过5. 输出中奖信息,包括中奖级别和奖金。这个程序使用了Random类生成随机数,采用数组存储号码,并使用基本方法判断中奖和包含情况。
package com.itheima.test;import java.util.Random;
import java.util.Scanner;
//用户号码随机版本=======================================================================================================
public class test10_1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);//随机生成6个红色中奖号码(1-33)和一个蓝色(1-16)中奖号码分别放在数组索引0-5和6:用方法int[] winningNumber = WinningNumber();//随机生成用户中奖号码int[] userNumber = WinningNumber();for (int i = 0; i < winningNumber.length; i++) {System.out.print(winningNumber[i] + " ");}System.out.println("\n_______________");for (int i = 0; i < userNumber.length; i++) {System.out.print(userNumber[i] + " ");}System.out.println("\n===========================");//判断蓝色球中奖次数int blueCount = 0;if (winningNumber[6] == userNumber[6]){blueCount = 1;}//判断红色球中奖个数int redCount = 0;for (int i = 0; i < userNumber.length-1; i++) {if (contains(winningNumber,userNumber[i])){redCount++;}}System.out.println("红球中奖"+redCount+"个,蓝球中奖"+blueCount+"个");if (redCount == 6 && blueCount == 1){System.out.println("一等奖:6000万");} else if (redCount == 6 && blueCount == 0) {System.out.println("二等奖:500万");} else if ((redCount == 5 && blueCount == 1) || (redCount == 5 && blueCount == 0)) {System.out.println("三等奖:3000元");} else if ((redCount == 4 && blueCount == 1) || (redCount == 4 && blueCount == 0)) {System.out.println("四等奖;200元");} else if ((redCount == 3 && blueCount == 1) || (redCount == 2 && blueCount == 1)) {System.out.println("五等奖:10元");} else if ((redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)) {System.out.println("六等奖:5元");}else{System.out.println("与奖擦肩而过");}}
//彩票号码生成public static int[] WinningNumber() {int[] Number = new int[7];Random r = new Random();for (int i = 0; i < Number.length - 1; ) {int red_Number = r.nextInt(33) + 1;if (contains(Number, red_Number) == false) {Number[i] = red_Number;i++;}}Number[6] = r.nextInt(16) + 1;return Number;}
//判断号码是否在数组中public static boolean contains(int[] arr, int number) {boolean contains = false;for (int i = 0; i < arr.length; i++) {if (number == arr[i]) {contains = true;break;}}return contains;}
}