栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java代码实现双色球小游戏2.0:完整代码(待完善)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java代码实现双色球小游戏2.0:完整代码(待完善)

package game;

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

public class DoubleBallTest {

	

	

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Random range = new Random();

		int[] red = new int[6];// 用于存储随机生成的6个红球
		int blue = 0;// 用于存储蓝球号

		int isgamblemoney = 0;// 用于存储用户输入的下注金额
		double bonus = 0.0;// 通过用户的下注得到若获奖得到的中奖金额

		double[] result = null;

		while (true) {
			int[] lottery = new int[7];// 用于存储最后选定的7个号码,每次循环要先清空原本数组的数据
			System.out.println("-------------------欢迎来到袁梦码双色球娱乐中心-----------------");
			System.out.println("-----------------------1.开始游戏--------------------------");
			System.out.println("-----------------------2.系统自动选号-----------------------");
			System.out.println("-----------------------3.一夜暴富-----------------------");
			System.out.println("-----------------------4.退出游戏--------------------------");
			System.out.print("请选择您的操作:");
			int selectVal = input.nextInt();
			switch (selectVal) {
			case 1:
				// 选择6个红球号码
				selectRedBall(input, lottery);
				// 选择篮球号码
				selectABlueBall(input, lottery);
				result = askingIsBetting(1, input, range, bonus, isgamblemoney);
				bonus = result[0];
				isgamblemoney = (int) result[1];
				// 系统自动产生7个球,并排除重复的红球
				blue = systemSelect(0, input, range, red, blue);
				break;
			case 2:
				// 系统自动产生7个球,并排除重复的红球
				blue = systemSelect(1, input, range, red, blue);
				for (int i = 0; i < red.length; i++) {
					lottery[i] = red[i];
				}
				lottery[6] = blue;
				// 下注
				result = askingIsBetting(2, input, range, bonus, isgamblemoney);
				bonus = result[0];
				isgamblemoney = (int) result[1];
				blue = systemSelect(0, input, range, red, blue);
				break;
			case 3:
				// 系统自动产生7个球,并排除重复的红球
				blue = systemSelect(2, input, range, red, blue);
				for (int i = 0; i < red.length; i++) {
					lottery[i] = red[i];
				}
				lottery[6] = blue;
				// 下注
				result = askingIsBetting(3, input, range, bonus, isgamblemoney);
				bonus = result[0];
				isgamblemoney = (int) result[1];
				blue = systemSelect(3, input, range, red, blue);
				break;
			case 4:
				System.out.println();
				System.out.println("******************退出成功,欢迎下次光临!!******************");
				System.out.println();
				return;
			default:
				System.out.println();
				System.out.println("******************输入的内容不符合要求,请重新输入!!******************");
				System.out.println();
				continue;
			}

			// 打印刚才自己选择的7个球号码,以及已经下注资金数
			System.out.println("您选择彩票号码是:" + Arrays.toString(lottery));
			System.out.println("已下注:" + isgamblemoney + "元");
			// 开奖结果
			getResult(lottery, red, blue, isgamblemoney, bonus);
		}

	}

//------------------------------------------------方法---------------------------------------------------

	
	public static void selectRedBall(Scanner input, int[] lottery) {
		System.out.println("请选择您的6个红色球号;");
		for (int i = 0; i < 6; i++) {
			while (true) {
				System.out.print("第" + (i + 1) + "个:");
				lottery[i] = input.nextInt();
				int resultIndex01 = myRemoveRed(lottery, lottery[i]);
				if (resultIndex01 == 2) {
					System.out.println();
					System.out.println("******************该值已重复,请重新输入!!******************");
					System.out.println();
					continue;
				} else {
					if (lottery[i] < 1 || lottery[i] > 33) {
						System.out.println();
						System.out.println("******************红球号码的范围为1-33!!******************");
						System.out.println();
						continue;
					}
				}
				break;
			}
		}
	}

	
	public static void selectABlueBall(Scanner input, int[] lottery) {
		System.out.print("请选择您的蓝色球号;");
		while (true) {
			lottery[6] = input.nextInt();
			if (lottery[6] < 1 || lottery[6] > 16) {
				System.out.println();
				System.out.println("******************蓝球的号码范围为:1-16!******************");
				System.out.println();
				System.out.print("请重新选择蓝球的号码:");
				continue;
			}
			break;
		}
	}

	
	public static double[] askingIsBetting(int flag, Scanner input, Random range, double bonus, int isgamblemoney) {
		while (true) {
			if (flag == 3) {
				isgamblemoney = 10000000;
				break;
			} else {
				System.out.println("请输入下注金额(1元、5元、10元、200元、3000元、5000000元、10000000元):");
				isgamblemoney = input.nextInt();
			}
			switch (isgamblemoney) {
			case 0:
				bonus = 0;
				break;
			case 1:
				bonus = isgamblemoney * 1;
				break;
			case 5:
				bonus = isgamblemoney * 2.5;
				break;
			case 10:
				bonus = isgamblemoney * 5;
				break;
			case 200:
				bonus = isgamblemoney * 100;
				break;
			case 3000:
				bonus = isgamblemoney * 1500;
				break;
			case 5000000:
				bonus = isgamblemoney * (range.nextInt(501) + 1500);
				break;
			case 10000000:
				bonus = isgamblemoney * (range.nextInt(501) + 1500);
				break;
			default:
				System.out.println();
				System.out.println("******************您输入的内容不符合要求,请重新输入!******************");
				System.out.println();
				continue;
			}
			break;
		}
		double[] result = new double[2];
		result[0] = bonus;
		result[1] = isgamblemoney;// 自动类型转换,在接收结果时,需要强转
		return result;
	}

	
	public static int systemSelect(int flag, Scanner input, Random range, int[] red, int blue) {

		if (flag == 0) {
			blue = myTools01(input, range, red, blue);
			System.out.println("红球开奖号码为:" + Arrays.toString(red));
			System.out.println("蓝球开奖号码为:" + blue);
		} else if (flag == 1) {
			blue = myTools01(input, range, red, blue);
			System.out.println("系统为您开的红球号码为:" + Arrays.toString(red));
			System.out.println("系统为您开的蓝球号码为:" + blue);
		} else if (flag == 2) {
			blue = myTools01(input, range, red, blue);
		} else if (flag == 3) {
			System.out.println("红球开奖号码为:" + Arrays.toString(red));
			System.out.println("蓝球开奖号码为:" + blue);
		}
		return blue;
	}

	
	public static int myTools01(Scanner input, Random range, int[] red, int blue) {
		System.out.println("");
		System.out.println("-------------$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$-------------------");
		blue = range.nextInt(16) + 1;// 用于存储蓝球号
		for (int i = 0; i < 6; i++) {
			red[i] = range.nextInt(33) + 1;
		}
//		排除红球号码有重复的数字
		for (int i = 0; i < red.length - 1; i++) {// 外层需要遍历4次;即范围是:0-3:4个下标;外层循环是要比较的次数
			int dest = red[i];
			for (int j = 1 + i; j < red.length; j++) {// 内层是从角标+1开始,不与自身比较了,范围为4,所以< 5
				if (dest == red[j]) {
					red[j] = range.nextInt(33) + 1;// 如果有重复的,就再自动生成一个随机数替换一个重复的值
				}
			}
		}
		return blue;
	}

	
	public static void getResult(int[] lottery, int[] red, int blue, int isgamblemoney, double bonus) {
		int rednumber = 0;// 用于记录红球中的个数
//		将自己选的红球号码与随机生成的号码进行比较
		for (int i = 0; i < lottery.length - 1; i++) {
			for (int j = 0; j < red.length; j++) {
				if (lottery[i] == red[j]) {
					rednumber++;
				}
			}
		}
		System.out.println("红球中奖的号码个数为:" + rednumber);
		if (lottery[6] == blue) {
			System.out.println("恭喜您:蓝色球号已中1");
		} else {
			System.out.println("很遗憾:蓝色球号没中0");
		}
		double money = 0;
		switch (rednumber) {
		case 0:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(六等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = 5 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = 5;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("没有中奖:经验证可以跳楼了");
			}
			break;
		case 1:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(六等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 5 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 5;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(六等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 5 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber * 5;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		case 2:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(六等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 5 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 5;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(六等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 5 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber * 5;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		case 3:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(五等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 10 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 10;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(五等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 10 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber * 10;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		case 4:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(四等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 200 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 200;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(五等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 10 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber * 10;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		case 5:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(三等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 3000 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 3000;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(四等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 200 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber * 200;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		case 6:
			if (lottery[6] == blue) {
				System.out.println("恭喜中奖(一等奖):中奖说明:中" + rednumber + "+" + "1");
				if (isgamblemoney == 1) {
					money = (rednumber + 1) * 10000000 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = (rednumber + 1) * 10000000;
					System.out.println("您的奖金为:" + money + "元");
				}
			} else {
				System.out.println("恭喜中奖(二等奖):中奖说明:中" + rednumber + "+" + "0");
				if (isgamblemoney == 1) {
					money = rednumber * 5000000 + bonus;
					System.out.println("您的奖金为:" + money + "元");
				} else {
					money = rednumber + 1 * 5000000;
					System.out.println("您的奖金为:" + money + "元");
				}
			}
			break;
		}
		System.out.println("-------------$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$-------------------");
		System.out.println("");
	}

	
	public static int myRemoveRed(int[] arr, int selectValue) {
		int count = 0;
		for (int i = 0; i < arr.length; i++) {
			if (selectValue == arr[i]) {
				count++;
			}
		}
		return count;
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号