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: