java基础练习之模拟类似双色球小系统
前言
使用java基础做一个模拟类似双色球的系统,该系统仅供参考,不是真实的双色球,娱乐而已,实现方法也不只这一种.
一、代码演示
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class TwoColorBall {
public static void main(String[] args) {
//用户选的的红球与篮球
int[] userRedBall=new int[6];
int userBlueBall=0;
//电脑随机生成的红球与篮球
int[] compRedBall=new int[6];
int compBlueBall=0;
Scanner input=new Scanner(System.in);
Random r=new Random();
System.out.println("请选择机选还是自选:(1:机选---2:自选)");
int temp=input.nextInt();
while (temp!=0){
switch (temp){
case 1:
//机选
ByContBall(userRedBall);
userBlueBall=r.nextInt(16)+1;
ByContBall(compRedBall);
compBlueBall=r.nextInt(16)+1;
ByIf(userRedBall,userBlueBall,compRedBall,compBlueBall);
temp=0;
break;
case 2:
//自选
System.out.println("请输入6个红球:");
for (int i=0;i<6;i++){
userRedBall[i]=input.nextInt();
}
System.out.println("请输入一个蓝球:");
userBlueBall=input.nextInt();
ByContBall(compRedBall);
compBlueBall=r.nextInt(16)+1;
ByIf(userRedBall,userBlueBall,compRedBall,compBlueBall);
temp=0;
break;
default:
System.out.println("请重新选择");
System.out.println("请选择机选还是自选:(1:机选---2:自选)");
temp=input.nextInt();
}
}
}
public static void ByContBall(int[] redBall){
int[] comp=new int[33];
for (int i=0;i<33;i++){
comp[i]=i+1;
}
//随机数工具类
Random r=new Random();
for (int i=0;i<redBall.length;i++){
//随机选择comp数组中的数据
int index=r.nextInt(comp.length-1-i);
redBall[i]= comp[index];
//将选择过的数据放到数组最后面,将最后面的数据向前方插入
comp[index]=comp[index]+comp[comp.length-1-i];
comp[comp.length-1-i]=comp[index]-comp[comp.length-1-i];
comp[index]=comp[index]-comp[comp.length-1-i];
}
}
//判断中奖情况
public static void ByIf(int[] userRed,int userBlue,int[] compRed,int compBlue){
Arrays.sort(userRed);
Arrays.sort(compRed);
int red=0,blue=0;
for (int i=0;i<userRed.length;i++){
for (int j=0;j<compRed.length;j++){
if (compRed[j]==userRed[i]){
red++;
break;
}
}
}
if (userBlue==compBlue){
blue+=1;
}
if (red<=4){
System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
System.out.println("您的选中为:红球"+red+"蓝球"+blue);
System.out.println("再接再厉!");
}else if (red==5){
System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
System.out.println("**********恭喜*************三等奖,奖金50万");
}else if (red==6 && blue==0){
System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
System.out.println("**********恭喜*************二等奖,奖金100万");
}else if (red == 6){
System.out.println("本期中奖号码:红球:"+Arrays.toString(compRed)+"蓝球:"+compBlue);
System.out.println("您的选号为:红球:"+Arrays.toString(userRed)+"蓝球"+userBlue);
System.out.println("**********恭喜*************一等奖,奖金1000万");
}
}
}
二.小结
本章概述了一个小系统,还是蛮有意思的,做好了不仅能提升java基础,还能闲着无聊来一注,碰一下运气,娱乐娱乐,仅供参考.
有哪里不足或者有更好的建议,欢迎留言吐槽,有哪里不懂的小伙伴,可以私信我,我会一一答复,感谢认可,感谢支持!