难点:
1.需要随机抽取一个数,直至它不与之前的数重复。
解决方法:死循环加信号位
package practice; import java.util.Random; import java.util.Scanner; public class practicedemo7 { //目标:双色球系统开发 public static void main(String[] args) { //3.引用方法,创立获奖码,七位,前六位是1-33间不重复值,第七位是1-16间不重复值 int[] arr=hujiangma(); //5.引用方法,打印创立的获奖码 arrprint(arr); //7.引用方法,创建用户猜的7位码 int[] brr=userguess(); arrprint(brr); //9.引用方法,判断是否中奖 judge(arr,brr); } //1.创建方法,随机产生七位不重复值 public static int[] hujiangma(){ Random sc= new Random(); int[] arr=new int[7]; for (int i = 0; i <arr.length-1; i++) { while(true) { int num = sc.nextInt(33) + 1; //0-32 +1 =1-33 boolean flag=true; for (int j = 0; j < i; j++) { if(arr[j]==arr[i]){ flag=false; break; } } if(flag){ arr[i]=num; break; } } } //2.随机产生一位1-16间的数 arr[6]=sc.nextInt(16)+1; return arr; } //4.创建方法,打印出创建的获奖码 public static void arrprint(int[] arr){ System.out.print("["); for (int i = 0; i < arr .length; i++) { System.out.print(i==arr.length-1?arr[i]:arr[i]+","); } System.out.println("]"); } //6.创建方法,显示用户猜的七个号 public static int[] userguess(){ int[] arr=new int[7]; Scanner sc=new Scanner(System.in); System.out.print("请输入猜的七个号:"); for (int i = 0; i < arr.length; i++) { arr[i]= sc.nextInt(); } return arr; } //8.判断中奖情况 public static void judge(int[] arr,int[] brr) { //看蓝球中了几个 int blue = 0; int red = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < brr.length - 1; j++) { if (arr[i] == brr[j]) { blue += 1; } } } //看红球中了几个 red = arr[6] == brr[6] ? 1 : 0; //开始算中了几等奖 if (red == 1 && blue < 3) { System.out.println("恭喜中了六等奖,共5元!"); } else if (red == 0 && blue == 4 || red == 1 && blue == 3) { System.out.println("恭喜中了五等奖,共10元!"); } else if (red == 0 && blue == 5 || red == 1 && blue == 4) { System.out.println("恭喜中了四等奖,共200元!"); } else if ( red == 1 && blue == 5) { System.out.println("恭喜中了三等奖,共3000元!"); }else if ( red == 0 && blue == 6) { System.out.println("恭喜中了二等奖,共500万!"); }else if ( red == 1 && blue == 6) { System.out.println("恭喜中了一等奖,共1000万!"); } } }