1 package pres.work; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 import java.util.Scanner; 6 7 / 8 * 实现双色球随机彩票 9 * @author three 10 * 11 */ 12 public class DoubleBall { 13 14 public static void main(String[] args) { 15 16 / 17 * 随机双色球,红球六个(不重复),蓝球一个 18 */ 19 int[] randomRedBall = new int[6]; 20 int[] randomBlueBall = new int[1]; 21 Random rand = new Random(); 22 for(int i = 0; i<randomRedBall.length; i++) { 23 int r= rand.nextInt(33)+1; 24 randomRedBall[i] = r; 25 //遍历,判断是否重复 26 for(int j = 0;j<i;j++) { 27 if(randomRedBall[j] == r) { 28 --i; 29 continue; 30 } 31 } 32 } 33 randomBlueBall[0] = rand.nextInt(16)+1; 34 35 / 36 * 键盘输入红/蓝球号码 相邻号码以空格隔开 37 */ 38 Scanner sc = new Scanner(System.in); 39 int[] setRedBall = new int[6]; 40 System.out.print("请输入选择的红球:"); 41 for(int i = 0; i<randomRedBall.length; i++) { 42 setRedBall[i] = sc.nextInt(); 43 } 44 System.out.print("请输入选择的蓝球:"); 45 int[] setBlueBall = new int[1]; 46 setBlueBall[0] = sc.nextInt(); 47 sc.close(); 48 49 //键盘输入结束后,打印出中奖号码,方便用户对比 50 System.out.printf("中奖红球号码 %s ",Arrays.toString(randomRedBall)); 51 System.out.printf("中奖蓝球号码 %s ",Arrays.toString(randomBlueBall)); 52 53 / 54 * 判断随机球与输入球号码是否相同,相同就加 1 55 */ 56 int sumRed = 0; 57 for(int i = 0; i<randomRedBall.length;i++) { 58 for(int j = 0; j<setRedBall.length;j++) { 59 if(randomRedBall[i] == setRedBall[j]) { 60 sumRed++; 61 } 62 } 63 } 64 int sumBlue = 0; 65 if(randomBlueBall[0] == setBlueBall[0]) ++sumBlue; 66 67 / 68 * 匹配中奖机制 69 */ 70 if(sumRed == 6 && sumBlue == 1) { 71 System.out.println("恭喜你获得:一等奖"); 72 }else if(sumRed == 6 && sumBlue == 0) { 73 System.out.println("恭喜你获得:二等奖"); 74 }else if(sumRed == 5 && sumBlue == 1) { 75 System.out.println("恭喜你获得:三等奖"); 76 }else if(sumRed == 5 && sumBlue == 0 || sumRed == 4 && sumBlue == 1) { 77 System.out.println("恭喜你获得:四等奖"); 78 }else if(sumRed == 4 && sumBlue == 0 || sumRed == 3 && sumBlue == 1) { 79 System.out.println("恭喜你获得:五等奖"); 80 }else if(sumRed == 2 && sumBlue == 1 || sumRed == 1 && sumBlue == 1 || sumRed == 0 && sumBlue == 1) { 81 System.out.println("恭喜你获得:六等奖"); 82 }else { 83 System.out.println("很遗憾,你没中奖。多买几次,总有机会的···"); 84 } 85 } 86 87 }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/17844.html