双色球问题
规则简介
简单分析
随机一组中奖号码的分析:
1. 中奖号码由6个红球和1个篮球组成(注意:6个红球要求不能重复)。
2. 可以定义方法用于返回一组中奖号码(7个数据),返回的形式是一个整型数。
代码如下
import java.util.Random;
import java.util.Scanner;public class BiColor {public static void main(String[] args) {int[] luckNumbers = creatLuckNumbers(); //随机一组中奖号码int[] userNumbers = userInputNumbers(); //用户输入一组双色球号judge(luckNumbers, userNumbers); //查看中奖与否printLuckNumbersAndUserNumbers(luckNumbers, userNumbers); //展示中奖号码与用户购买号码}//1.随机一组中奖号码返回public static int[] creatLuckNumbers() {int[] luckNumbers = new int[7];boolean[] flag = new boolean[36]; //定义一个标记数组Random random = new Random();int luckNumber;for (int i = 0; i < luckNumbers.length - 1; i++) { //产生六个不同的随机数存入数组do { //循环产生不同的随机数luckNumber = random.nextInt(33) + 1;} while (flag[luckNumber]);flag[luckNumber] = true;luckNumbers[i] = luckNumber;}luckNumber = random.nextInt(16) + 1; //产生蓝色球的号码luckNumbers[luckNumbers.length-1] = luckNumber;return luckNumbers;}//用户输入一组双色球号返回public static int[] userInputNumbers() {System.out.println("请选择您想要的球注(请在1~36间选择一个数,一共输入7次):");int[] userNumbers = new int[7];Scanner scanner = new Scanner(System.in);for (int i = 0; i < userNumbers.length; i++) {int userNumber = scanner.nextInt();//检查数据的合法性userNumber = checkNumber(userNumber,i);userNumbers[i] = userNumber;}return userNumbers;}//传入两组号码,判断用户中奖情况public static void judge(int[] luckNumbers, int[] userNumbers) {int count = 0;boolean blue = false;for (int i = 0; i < luckNumbers.length - 1; i++) { //计算红球猜中的次数if (userNumbers[i] == luckNumbers[i]) {count++;}}if (luckNumbers[6] == userNumbers[6]) {blue = true; //猜中蓝色球}if (count == 7 & blue) {System.out.println("恭喜您可以少奋斗10年,喜提1000万大奖!");} else if (count == 6) {System.out.println("恭喜您可以少奋斗5年,喜提500万大奖!");} else if (count == 5 & blue) {System.out.println("恭喜您中了三等奖,喜提3000元!");} else if (count == 5) {System.out.println("恭喜您中了四等奖,喜提200元!");} else if (count == 4 & blue) {System.out.println("恭喜您中了四等奖,喜提200元!");} else if (count == 4) {System.out.println("恭喜您中了五等奖,喜提10元!");} else if (count == 3 & blue) {System.out.println("恭喜您中了五等奖,喜提10元!");} else if (count == 2 & blue) {System.out.println("恭喜您中六等奖,喜提5元!");} else if (count == 1 & blue) {System.out.println("恭喜您中六等奖,喜提5元!");} else if (blue) {System.out.println("恭喜您中六等奖,喜提5元!");} else {System.out.println("很感谢您为体彩的发展贡献的一份力量,欢迎下次光临~~~");}}//检查数据的合法性public static int checkNumber(int userNumber, int i) {Scanner scanner = new Scanner(System.in);if(i<6){ //红色球号码的范围while (userNumber > 33 || userNumber <= 0) {System.out.println("请在1~33(包括边界)间选择一个数:");userNumber = scanner.nextInt();}}else{ //蓝色球号码的范围while (userNumber > 16 || userNumber <= 0) {System.out.println("请在1~16(包括边界)间选择一个数:");userNumber = scanner.nextInt();}}return userNumber;}//展示中奖号码与用户购买号码public static void printLuckNumbersAndUserNumbers(int[] luckNumbers, int[] userNumbers) {System.out.println("本次开奖结果:");for (int i = 0; i < luckNumbers.length; i++) {System.out.print(luckNumbers[i] + " ");}System.out.println("\t");System.out.println("您购买的号码为:");for (int i = 0; i < userNumbers.length; i++) {System.out.print(userNumbers[i] + " ");}}
}
简单补充
这是黑马程序员Java基础的一道练习题,自己简单的一些想法和代码展示,有很多不足,望指正。