双色球彩票系统:
需求:投注号码由6个红色号码和1个蓝色球号码组成。红色球号码从1-33中选择,蓝色球号码从1-16当中选择 * 红 蓝 * 一等奖 6 1 * 二等奖 6 0 * 三等奖 5 1 * 四等奖 5 0 * 4 1 * 五等奖 4 0 * 3 1 * 2 1 * 六等奖 1 1 * 0 1
分别给自定义自己的每种奖项,给予不同的现金奖励!!!!
package demo;import java.util.Random;
import java.util.Scanner;/*
* 案例:双色球彩票系统
* 需求:投注号码由6个红色号码和1个蓝色球号码组成。红色球号码从1-33中选择,蓝色球号码从1-16当中选择
* 红 蓝
* 一等奖 6 1
* 二等奖 6 0
* 三等奖 5 1
* 四等奖 5 0
* 4 1
* 五等奖 4 0
* 3 1
* 2 1
* 六等奖 1 1
* 0 1
* */
public class UnionLottoLotteryTicket {public static void main(String[] args) {//1.随机生成7个中奖号码int [] arrprize = createNumber();System.out.println("==========================");for (int i = 0; i < arrprize.length; i++) {System.out.print(arrprize[i]+" ");}System.out.println("\n"+"==========================");//2.让用户去输入彩票号码int [] arrUser =UserInputNumber();//3.判断中奖的情况int redcount = 0;int bluecount = 0;//判断红球的中奖情况for (int j = 0; j < arrprize.length-1; j++) {int rednumber = arrUser[j];for (int k = 0; k < arrprize.length - 1;k++ ) {if(rednumber == arrprize[k]){redcount++;//如果找到就判断下个红球号码是否中奖break;}}}//判断篮球的中奖情况while (true) {int bluenumber = arrUser[arrUser.length-1];if(bluenumber == arrprize[arrprize.length-1]){bluecount++;break;}}//判断是否中奖if(redcount==6&&bluecount==1){System.out.println("恭喜你中奖1000万元");}else if(redcount == 6&&bluecount==0){System.out.println("恭喜你中奖500万元");}else if(redcount == 5&&bluecount == 1){System.out.println("恭喜你中奖3000元");}else if((redcount==5&&bluecount==0)||(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("谢谢参与,谢谢惠顾!");}}/** 随机创建7个中奖号码* */public static int[] createNumber(){//声明数组去保存获奖的号码有7个好中奖号码int [] arr = new int[7];//先获取红球的获奖号码Random r = new Random();for (int i = 0; i < arr.length -1; ) {//能取大不取小所以需要加1int redNumber= r.nextInt(33)+1;boolean flag = contains(arr,redNumber);if(!flag){arr[i] = redNumber;i++;}}//随机生成篮球的中奖号码int blueNumber = r.nextInt(16)+1;arr[arr.length-1] = blueNumber;return arr;}/** 判断红球有没有重复被抽到过* */public static boolean contains(int [] arr,int number){for (int i = 0; i < arr.length; i++) {if(arr[i] == number){return true;}}return false;}/** 输入用户购买的彩票* */public static int[] UserInputNumber(){//声明数组去保存7个彩票号码int [] arr = new int[7];Scanner sc = new Scanner(System.in);//输入用户的红球号码for (int i = 0; i < arr.length-1;) {System.out.println("请输入您的第"+(i+1)+"个号码");int tempred = sc.nextInt();if(tempred>=1&&tempred<=33){boolean flag = contains(arr,tempred);if(!flag){arr[i] = tempred;i++;}else {System.out.println("您输入的号码已经存在请重新输入");}}else{System.out.println("您输入的号码有误,请重新输入");}}//输入用户的篮球号码System.out.println("请输入篮球号码:");while (true) {int tempblue = sc.nextInt();if(tempblue>=1&&tempblue<=16){arr[arr.length-1] = tempblue;break;}else{System.out.println("您输入的号码有误,去重新输入");}}return arr;}}
运行结果: