【Java基础10】模拟双色球综合案例

/* 模拟双色球综合案例
1、用户选择手选号码还是机选号码
2、接收用户选择的号码(6个红球,1个篮球)
3、生成系统选择的号码(6个红球,1个篮球)
4、比较用户和系统选择的号码,并记录中奖个数
5、验证是否中奖
6、系统号码排序
7、公布结果
*/

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

/*  模拟双色球综合案例
 * 1、用户选择手选号码还是机选号码
 * 2、接收用户选择的号码(6个红球,1个篮球)
 * 3、生成系统选择的号码(6个红球,1个篮球)
 * 4、比较用户和系统选择的号码,并记录中奖个数
 * 5、验证是否中奖
 * 6、系统号码排序
 * 7、公布结果 
 * */

public class Test02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] userRedBall=new int[6];//用户选择的红球
		int userBlueBall=0;//用户选择的篮球
		int[] systemRedBall=new int[6];//系统选择的红球
		int systemBlueBall=0;//系统选择的篮球
		int redCount=0;//记录红球个数
		int blueCount=0;//记录篮球个数
		int[] redBall=new int[33];//存储1-33的红球号码
		int[] blueBall=new int[16];//存储1-16的篮球号码;
		Random r=new Random();
		//随机生成6个1-33的不重复的红球数
		for(int i=0;i<redBall.length;i++){
			redBall[i]=i+1;
		}
		
		//游戏开始,系统提示
		System.out.println("双色球游戏已开始,祝您游戏愉快!");
		System.out.println("请问您是要机选还是手选号码?1、手选号码,2、机选号码(输入序号即可)");
		
		Scanner input=new Scanner(System.in);
		
		boolean flag=true;
		while(flag){
			int isSelect=input.nextInt();
			
			switch(isSelect){
				case 1:
					System.out.println("请输入您选择的6个红球号码(不重复):");
					for(int i=0;i<userRedBall.length;i++){
						userRedBall[i]=input.nextInt();
					}
					System.out.println("请输入您选择的1个蓝球号码:");
					userBlueBall=input.nextInt();
					flag=false;
					break;
				case 2:
					systemSelect(redBall,userRedBall);
					userBlueBall=r.nextInt(16)+1;
					System.out.println("系统已自动生成号码!");
					flag=false;
					break;
			}
		}
		
		//系统随机生成号码
		systemSelect(redBall,systemRedBall);
		systemBlueBall=r.nextInt(16)+1;
		
		//统计结果
		//统计红球
		for(int i=0;i<userRedBall.length;i++){
			for(int j=0;j<systemRedBall.length-redCount;j++){
				if(userRedBall[i]==systemRedBall[i]){
					int temp=systemRedBall[j];
					systemRedBall[j]=systemRedBall[systemRedBall.length-1-redCount];
					systemRedBall[systemRedBall.length-1-i]=temp;
					redCount++;
					break;
				}
			}  
		}
		//统计篮球
		if(systemBlueBall==userBlueBall){
			blueCount++;
		}
		
		//验证是否中奖
		if(blueCount==0&&redCount<=3){
			System.out.println("很遗憾您未中奖,谢谢您的参与!");
		}else if(blueCount==1&&redCount<3){
			System.out.println("恭喜您中了六等奖,5元!");
		}else if((blueCount==1&&redCount==3)||(blueCount==0&&redCount==4)){
			System.out.println("恭喜您中了五等奖,10元!");
		}else if((blueCount==1&&redCount==4)||(blueCount==0&&redCount==5)){
			System.out.println("恭喜您中了四等奖,200元!");
		}else if(blueCount==1&&redCount==5){
			System.out.println("恭喜您中了三等奖,3000元!");
		}else if(blueCount==0&&redCount==6){
			System.out.println("恭喜您中了二等奖,150W!");
		}else if(blueCount==1&&redCount==6){
			System.out.println("恭喜您中了一等奖,500W!");
		}else{
			System.out.println("系统有误,中奖无效!");
		}
		
		System.out.println();
		
		//公布系统号码
		System.out.println("本期中奖红球号码为:");
		Arrays.sort(systemRedBall);
		System.out.println(Arrays.toString(systemRedBall));
		System.out.println("本期中奖蓝球号码为:"+systemBlueBall);
		
		System.out.println();
		
		//公布用户号码
		System.out.println("您选择的红球号码为:");
		Arrays.sort(userRedBall);
		System.out.println(Arrays.toString(userRedBall));
		System.out.println("您选择的蓝球号码为:"+userBlueBall);
		
		System.out.println();
		
		System.out.println("感谢您购买双色球,祝您生活愉快!");
		
	}
	
	
	//机器生成多个不重复的数
	public static void systemSelect(int[] redBall,int[] userRedBall){
		Random r=new Random();
		int index=-1;
		for(int i=0;i<userRedBall.length;i++){
			index=r.nextInt(redBall.length-i);
			userRedBall[i]=redBall[index];
			//将随机生成的数与最后一个数交换
			int temp=redBall[index];
			redBall[index]=redBall[redBall.length-1-i];
			redBall[redBall.length-1-i]=temp;
			
		}
	}
		

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值