Java之旅 双色球模拟案列

该程序使用Java实现了一个双色球彩票的选号系统,包括机选和自选两种方式。用户可以选择机选,由系统生成6个红球和1个蓝球号码,或者自选红球号码并输入蓝球号码。程序会对比用户的投注号码与系统生成的中奖号码,输出中奖情况。
摘要由CSDN通过智能技术生成

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

public class double_color_ball{
	public static void main(String[] args){
		//定义相关变量
		int[] RedBall = new int[33];
 		int[] UserSelectRed = new int[6];
		int UserSelectBule = 0;
		int[] SystemSelectRed = new int[6];
		int SystemSelectBule = 0;		
		int BuleCount = 0;//中奖蓝球数
		int RedCount = 0;//中奖红球球
		boolean flat = true;
		
		System.out.println("双色球游戏开始:");
		Scanner input = new Scanner(System.in);
		
		
		/*以下为选号实现*/
		//生成33个随机数字的数列
		for(int i=0;i<33;i++)
			RedBall[i]=i+1;
		
		while(flat){
			System.out.println("请选择机选数字/自选数字:1/2");
			int IsAuto = input.nextInt();
			Random r = new Random();
			switch(IsAuto){
				case 1:
					SysSelectRed(RedBall,UserSelectRed);
					UserSelectBule = r.nextInt(16);
					flat = false;
				break;
				case 2:
					UserRed(UserSelectRed);
					while(UserSelectBule == 0){
						System.out.println("请输入您选择的蓝球号码:");
						int UserRedBall = input.nextInt();
						if(UserRedBall<=16)
						UserSelectBule = UserRedBall;
						else System.out.println("输入有误,请重新输入");
					}
					flat = false;
					break;
				default:
					flat = true;				
			}		
		}
		
		
		/*以下为系统生成中奖号码*/
		SysSelectRed(RedBall,SystemSelectRed);
		Random r = new Random();
		SystemSelectBule = r.nextInt(16);
		
		
		/*以下为中奖号码匹配*/
		for(int i =0;i<UserSelectRed.length;i++){
			for(int j = 0;j<SystemSelectRed.length;j++)
				if(UserSelectRed[i]==SystemSelectRed[j])
					RedCount++;
		}
		if(UserSelectBule==SystemSelectBule)
			BuleCount = 1;
		
		
		/*以下为公布中奖情况*/
		Arrays.sort(UserSelectRed);
		Arrays.sort(SystemSelectRed);
		System.out.print("中奖号码为:红球: ");
		for(int x:SystemSelectRed)
			System.out.print(x+" ");
		System.out.println("蓝球: "+SystemSelectBule);
		System.out.print("您的投注号码为:红球");
		for(int x:UserSelectRed)
			System.out.print(x+" ");
		System.out.println("蓝球: "+UserSelectBule);
		System.out.println("红球中奖个数为:"+RedCount+",蓝球中奖个数为:"+BuleCount);
	}
	
	
		//机选数字的方法
	public static void SysSelectRed(int[] Ball,int[] SysSelect){
		int p=-1;  //用来放随机生成数的下标
		Random r = new Random();
		for(int i=0;i<SysSelect.length;i++){
			p = r.nextInt(Ball.length-i);
			int temp = Ball[p];
			SysSelect[i]=temp;
			Ball[p]=Ball[Ball.length-1-i];
			Ball[Ball.length-1-i]=temp;
		}
	}
	//自选数字方法
	public static void UserRed(int[] UserRedNum){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入您选择的红球号码:");
		for(int i=0;i<UserRedNum.length;i++){
			UserRedNum[i]=input.nextInt();
		}
			
	}
}

 调试调了近一个小时,还是得多写/(ㄒoㄒ)/~~

命名等方面需规范

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值