Java study day four --- A Welfare lottery‘s Demo (Watching the video once and writing it out)

Code:

package doubleBall;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class DoubleBall {
	// 实例变量
	private byte[] redBallAll = new byte[33];
	private byte[] blueBallAll = new byte[16];
	private byte[] userRedBall = new byte[6];
	private byte[] userBlueBall = new byte[1];
	private byte[] sysRedBall = new byte[6];
	private byte[] sysBlueBall = new byte[1];
	private byte redCount = 0;
	private byte blueCount = 0;
	// 构造函数
	private DoubleBall() {
		// 赋值33个原始红球
		for (byte i = 0; i < this.redBallAll.length; i++) {
			redBallAll[i] = (byte) (i + (byte)(1));
		}
		// 赋值16个原始蓝球
		for (byte i = 0; i < this.blueBallAll.length; i++) {
			this.blueBallAll[i] = (byte) (i + (byte)(1));
		}
	}
	
	// 机选和自选判断中枢
	private byte autoGetChoice(Scanner input) {
		do {
			try {
				byte choice = input.nextByte();
				if (choice < 0 || choice > 1) {
					throw new Exception();
				}
				return choice;
			}catch (Exception exc){
				System.out.print("输入的值错误!请重新输入: ");
				// 这个地方为什么要这么处理
				input = new Scanner(System.in);
				continue;
			}
		}while(true);
	}
	// 机选, 计算球号,赋值给指定数组, 排序, 代参为了代码复用
	private void autoChoice_InFunc(byte[] typeBall, byte[] getArray, Random r) {
		for (byte i = 0; i < getArray.length; i++) {
			byte tempBallIndex = (byte) r.nextInt(typeBall.length - i);
			getArray[i] = typeBall[tempBallIndex];
			// 交换
			typeBall[typeBall.length - 1 - i] += typeBall[tempBallIndex];
			typeBall[tempBallIndex] = (byte) (typeBall[typeBall.length - 1 - i] - typeBall[tempBallIndex]);
			typeBall[typeBall.length - 1 - i] -= typeBall[tempBallIndex];
		}
		this.bubble_sort(getArray);
	}
	
	// 冒泡排序,代参为了代码复用
	private void bubble_sort(byte[] sortArray) {
		for (byte i = 0; i < sortArray.length - 1; i++) {
			for (byte j = 0; j < sortArray.length - 1 - i; j++) {
				if (sortArray[j] > sortArray[j + 1]) {
					sortArray[j] += sortArray[j + 1];
					sortArray[j + 1] = (byte)(sortArray[j] - sortArray[j + 1]);
					sortArray[j] = (byte)(sortArray[j] - sortArray[j + 1]);
				}
			}
		}
	}
	// 机选函数处理中枢
	private void autoChoice() {
		Random r = new Random();
		// 机选红球
		this.autoChoice_InFunc(this.redBallAll, this.userRedBall, r);
		// 机选蓝球
		this.autoChoice_InFunc(this.blueBallAll, this.userBlueBall, r);
	}
	
	// 自选, 保持和机选函数参数一致性
	private void handGetChoice(byte[] typeBall, byte[] getArray, Scanner input) {
		String ballFlag = "";
		if (getArray.length > 2) {
			ballFlag = "红";
		}else {
			ballFlag = "蓝";
		}
		byte count = 0;
		byte key = 0;
		while(count < getArray.length) {
			System.out.print("请输入你的第" + (count + 1) + "个" + ballFlag + "球数字(" + 1 + "-" 
									+ typeBall.length + ")共" + getArray.length + "个:");
			try {
				key = input.nextByte();
				if (key < 0 || key > typeBall.length) {
					throw new Exception();
				}
			}catch (Exception exc) {
				System.out.println("输入错误,请重新输入!");
				// 这个地方为什么要这么处理
				input = new Scanner(System.in);
				continue;
			}
			byte[] cloneGetArray = getArray.clone();
			this.bubble_sort(cloneGetArray);
			if (this.dichotomySort(cloneGetArray, key) == -1) {
				getArray[count] = key;
				count++;
			}
		}
		this.bubble_sort(getArray);
	}
	// 二分法查找
	private byte dichotomySort(byte[] arraySelect, byte key) {
		byte middle = 0;
		byte start = 0;
		byte end = (byte)(arraySelect.length - 1);
		for (byte i = 0; i < arraySelect.length; i++) {
			middle = (byte)((start + end) / 2);
			if (key > arraySelect[middle]) {
				start = (byte)(middle + 1);
			}else if(key < arraySelect[middle]) {
				end = (byte)(middle - 1);
			}else if(key == arraySelect[middle]){
				return middle;
			}
		}
		return -1;
	}
	// 自选
	private void manualChoice() {
		Scanner input = new Scanner(System.in);
		this.handGetChoice(this.redBallAll, this.userRedBall, input);
		this.handGetChoice(this.blueBallAll, this.userBlueBall, input);
	}
	// 选中号码数量统计
	private void selectedNum() {
		Random r = new Random();
		this.autoChoice_InFunc(this.redBallAll, this.sysRedBall, r);
		this.autoChoice_InFunc(this.blueBallAll, this.sysBlueBall, r);
		for (byte i = 0; i < this.sysRedBall.length; i++) {
			if (this.dichotomySort(this.userRedBall, this.sysRedBall[i]) != -1) {
				this.redCount++;
			}
		}
		for (byte i = 0; i < this.sysBlueBall.length; i++) {
			if (this.dichotomySort(this.userBlueBall, this.sysBlueBall[i]) != -1) {
				this.blueCount++;
			}
		}
	}
	private void winPrize() {
		if (this.redCount == 6) {
			if (this.blueCount == 1) {
				System.out.print("您中了一等奖!");
			}else {
				System.out.print("您中了二等奖!");
			}
		}else if(this.redCount == 5) {
			if (this.blueCount == 1) {
				System.out.print("您中了三等奖!");
			}else {
				System.out.print("您中了四等奖!");
			}
		}else if(this.redCount == 4) {
			if (this.blueCount == 1) {
				System.out.print("您中了四等奖!");
			}else {
				System.out.print("您中了五等奖!");
			}
		}else if(this.redCount == 3) {
			if (this.blueCount == 1) {
				System.out.print("您中了五等奖!");
			}else {
				System.out.println("感谢你为公益事业献出一份力量");
			}
		}else {
			if (this.blueCount == 1) {
				System.out.print("您中了六等奖!");
			}else {
				System.out.println("感谢你为公益事业献出一份力量");
			}
		}
	}
	// main function
 	public void start() {
		System.out.print("请输入数字机选0、自选1:");
		Scanner input = new Scanner(System.in);
		// 判断运行模式,机选、自选
		switch(this.autoGetChoice(input)) {
			// choice go through Machine
			case 0:
				this.autoChoice();
				break;
			// choice manually
			case 1:
				this.manualChoice();
		}
		System.out.println("彩票红球:" + Arrays.toString(this.userRedBall));
		System.out.println("彩票蓝球: " + Arrays.toString(this.userBlueBall));
		this.selectedNum();
		System.out.println("开奖红球: " + Arrays.toString(this.sysRedBall));
		System.out.println("开奖蓝球: " + Arrays.toString(this.sysBlueBall));
		this.winPrize();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DoubleBall demo = new DoubleBall();
		demo.start();
	}

}

result:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
大学生就业服务平台管理系统按照操作主体分为管理员和用户。管理员的功能包括学生档案管理、字典管理、试卷管理、试卷选题管理、试题表管理、考试记录表管理、答题详情表管理、错题表管理、法律法规管理、法律法规收藏管理、法律法规留言管理、就业分析管理、论坛管理、企业管理、简历管理、老师管理、简历投递管理、新闻资讯管理、新闻资讯收藏管理、新闻资讯留言管理、学生信息管理、宣传管理、学生管理、职位招聘管理、职位收藏管理、招聘咨询管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生就业服务平台管理系统可以提高大学生就业服务平台信息管理问题的解决效率,优化大学生就业服务平台信息处理流程,保证大学生就业服务平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理新闻信息,管理大学生就业服务平台信息,包括考试管理,培训管理,投递管理,薪资管理等,可以管理新闻信息。 考试管理界面,管理员在考试管理界面中可以对界面中显示,可以对考试信息的考试状态进行查看,可以添加新的考试信息等。投递管理界面,管理员在投递管理界面中查看投递种类信息,投递描述信息,新增投递信息等。新闻信息管理界面,管理员在新闻信息管理界面中新增新闻信息,可以删除新闻信息。新闻信息类型管理界面,管理员在新闻信息类型管理界面查看新闻信息的工作状态,可以对新闻信息的数据进行导出,可以添加新新闻信息的信息,可以编辑新闻信息信息,删除新闻信息信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值