游戏规则:
1-33号红球你可以选择6个球号,1-16号蓝球你可以选择1个球号,也可利用系统随机生成,然后利用系统生成红球与蓝球,在进行比较,球号相同数越多,奖励越高哦!
主要功能就是 :随机数生成不重复。
//代码可优化
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class DichroicBall {
Random rd = new Random();
static int[] userRedball = new int[6];// 保存用户选择红球数
static int userBlueball = 0;
static int[] sysRedball = new int[6];// 保存系统选择红球数
static int sysBlueball = 0;
int userCorrectRedBall = 0;// 记录正确个数
int userCorrectBlueBall = 0;
int Deposit[] = new int[33];// 1-33
public static void main(String[] args) {
DichroicBall db = new DichroicBall();
db.function();
db.function2();
db.compareOf(sysRedball, userRedball, userBlueball, sysBlueball);
db.sysOutput(sysRedball);
db.userOutput(userRedball);
}
public DichroicBall() {// 初始化1-33红球号
for (int i = 0; i < Deposit.length; i++) {
Deposit[i] = i + 1;
}
}
public void sysOutput(int ball[]) {// 系统生成红球数组输出
System.out.println("Winning the prize in this issue red Ball Number:");
Sort(ball);
System.out.println(Arrays.toString(ball));
System.out.println("Winning the prize in this issue blue Ball Number:" + sysBlueball);
}
public void userOutput(int ball[]) {// 用户生成红球数组输出
System.out.println("Red Ball of Your Choice:");
Sort(ball);
System.out.println(Arrays.toString(ball));
System.out.println("Blue Ball of Your Choice:" + userBlueball);
}
public void Sort(int ball[]) {//排序算法
for (int i = 0; i < ball.length - 1; i++) {// 冒泡排序
for (int j = 0; j < ball.length - 1 - i; j++) {
if (ball[j] > ball[j + 1]) {
ball[j] = ball[j + 1] + ball[j];
ball[j + 1] = ball[j] - ball[j + 1];
ball[j] = ball[j] - ball[j + 1];
}
}
}
}
public void compareOf(int[] sys, int[] user, int a, int b) {//用户球数与系统球数比较
int len = sys.length;
int len2 = user.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len2; j++) {// 比较算法可以优化
if (sys[i] == user[j]) {
userCorrectRedBall++;
}
}
}
System.out.println("Number of Red Ball Winners:" + userCorrectRedBall);
if (a == b)//蓝球比较
userCorrectBlueBall = 1;
System.out.println("Number of Blue Ball Winners:" + userCorrectBlueBall);
if (userCorrectBlueBall == 0 && userCorrectRedBall < 3) {
System.out.println("Sorry,you didn't win the prize,Please purchase the next issue.");
} else if (userCorrectRedBall <= 3) {
System.out.println("congratulation! Third Award.");
} else if (userCorrectBlueBall == 1 && userCorrectRedBall < 6) {
System.out.println("congratulation! Second award.");
} else if (userCorrectBlueBall == 1 && userCorrectRedBall == 6) {
System.out.println("congratulation! The first prize.");
} else {
System.out.println("System crash,no charge for this time.");
}
}
public void function2() {
// 系统随机生成红蓝球
computerSelection(Deposit, sysRedball);
sysBlueball = rd.nextInt(16) + 1; //1-16
}
public void function() {
System.out.println("Two-color ball game begins,please choose:");
System.out.println("1:systemselect 2:uesrselect");
String num = JOptionPane.showInputDialog("please input 1 or 2:");
int num2 = Integer.parseInt(num);
while (true) {//可容错
if (num2 == 1) {
System.out.println("your choice is systemselect");
computerSelection(Deposit, userRedball);// 机选红球
userBlueball = rd.nextInt(16) + 1;// 机选蓝球
break;
} else if (num2 == 2) {
System.out.println("your choice is uesrselect");
System.out
.println("please selection six red ball number(1-33):");
int len = userRedball.length;
for (int i = 0; i < len; i++) {
userRedball[i] = new Scanner(System.in).nextInt();
}
System.out
.println("please selection one blue ball number(1-16):");
userBlueball = new Scanner(System.in).nextInt();
break;
} else {
System.out.println("fatal error,please input again:");
num = JOptionPane.showInputDialog("please input 1 or 2:");
num2 = Integer.parseInt(num);
}
}
}
// 核心技术,随机数不重复
public void computerSelection(int[] Redball, int[] userRedball) {
int len = Redball.length;// 33
int len2 = userRedball.length;// 6
int index = -1;// 下标值
for (int i = 0; i < len2; i++) {// 循环6次
index = rd.nextInt(len - i);// 每次减去最后一个,防止随机重复: 1-33 1-32 1-31
userRedball[i] = Redball[index];// 先保存在6个红球中,然后将这个球与最后交换位置即可
int temp = Redball[index];// temp保存要移动的数
Redball[index] = Redball[len - 1 - i];// 生成数与(1-33)最后一个交换位置,防止随机重复
Redball[len - 1 - i] = temp;//完成交换
// 完成后数组 Deposit被打乱
}
}
}