public class Damo_09_双色球 { public static void main(String[] args) { jiang(red() , luck()); System.out.println(Arrays.toString(luck())); } public static int[] red(){ // 这里是键盘录入红球 int[] arr = new int[7]; // 一共7个球 6红1蓝 所以先定下长度为7的int类型数组 Scanner sc = new Scanner(System.in); // 预留蓝球位置 最后一个位置就是蓝球 for (int i = 0; i < arr.length - 1;) { // -1 是预留最后一个 篮球 System.out.println("请输入第" + (i + 1) + "颗红球"); int a = sc.nextInt(); if (a >= 1 && a <= 33){ // 红球的范围为1-33 if (panduan(arr , a) == -1){ // panduan 这个是下面写的panduan方法 用于判断该数字是否重复 不重复返回 -1; arr[i] = a; i++; // 随机出来的a成功赋值进入数组中后 再进行i++; }else{ System.out.println("您的数字输入有误,请重新输入范围为1-33的数字并且数字不能重复~~~"); } }else { System.out.println("您的数字输入有误,请重新输入范围为1-33的数字并且数字不能重复~~~"); } } System.out.println("请输入最后一位蓝球~"); // 篮球为1-16 不与红球数字冲突 while (true){ int a1 = sc.nextInt(); if (a1 >= 1 && a1 <= 16){ arr[arr.length-1] = a1; break; }else { System.out.println("您的数字输入有误,请重新输入范围1-16的数字~~~"); } } return arr; } //此方法用于判断键盘录入的红球数字是否重复 public static int panduan(int[] arr , int num){ for (int i = 0; i < arr.length; i++) { if (arr[i] == num){ return i; } } return -1; } // 随机生成的中奖号码 public static int[] luck(){ int[] arr2 = new int[7]; Random r = new Random(); for (int i = 0; i < arr2.length - 1;) { int red = r.nextInt(33) + 1; if (panduan(arr2 , red) == -1){ arr2[i] = red; i ++; } } int blue = r.nextInt(16) + 1; arr2[arr2.length - 1] = blue; return arr2; } public static void jiang(int[] arr , int[] arr1){ int sum = 0; //录入的号码和随机的号码进行对比 看一样的红球有多少个 ,篮球另外判断 for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1; j++) { if (arr[i] == arr1[j]){ sum ++; } } } // 这个if判断是判断篮球是否一样 if (arr[6] == arr1[6]){ switch (sum){ // 红球中奖个数 0 1 2 个的金额 下面以此类推 case 0 , 1 , 2 : System.out.println("恭喜您中了六等奖~喜提5元"); break; case 3: System.out.println("恭喜您中了五等奖~喜提10元"); break; case 4: System.out.println("恭喜您中了四等奖~喜提200元"); break; case 5: System.out.println("恭喜您中了三等奖~喜提3000元"); break; case 6: System.out.println("恭喜您中了一等奖~喜提10000000元"); break; } // 篮球没中 但红球中的个数小于或等于3 也是无奖 }else if (sum <= 3){ System.out.println("谢谢惠顾~您没中奖~~~"); }switch (sum){ case 4: System.out.println("恭喜您中了五等奖~喜提10元"); break; case 5: System.out.println("恭喜您中了四等奖~喜提200元"); break; case 6: System.out.println("恭喜您中了二等奖~喜提5000000元"); break; } } }
双色球简单案例
最新推荐文章于 2024-06-05 21:00:00 发布