Java 模拟福彩双色球

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

public class Test01 {
	
	public static void main(String[] args) {
		game lottery = new game();
		lottery.welcome();
		Scanner sca = new Scanner(System.in);
		
		while(true){
			int n=sca.nextInt();
			if(n==1){
				lottery.buyLottery();
			}
			if(n==2){
				lottery.getLottery();
			}
			if(n==3){
				break;
			}
			lottery.welcome();
		}
		System.out.println("*****退出,感谢使用*****");
		sca.close();
	}
}
class game{
	
	private int buyMoney;
	private int getMoney;
	private int[] buyRed;
	private int buyBlue;
	private int[] lotteryRed;
	private int lotteryBlue;
	
	public game() {
		this.buyMoney = 0;
		this.getMoney = 0;
		buyRed=null;
		lotteryRed=null;
	}
	public void countMoney(int redCount){
		if(redCount==6&&buyBlue==lotteryBlue){
			System.out.println("一等奖");
			getMoney+=5000000;
		}
		else if(redCount==6&&buyBlue!=lotteryBlue){
			System.out.println("二等奖");
			getMoney+=1000000;
		}
		else if(redCount==5&&buyBlue==lotteryBlue){
			System.out.println("三等奖");
			getMoney+=3000;
		}
		else if((redCount==5&&buyBlue!=lotteryBlue)||(redCount==4&&buyBlue==lotteryBlue)){
			System.out.println("四等奖");
			getMoney+=200;
		}
		else if((redCount==4&&buyBlue!=lotteryBlue)||(redCount==3&&buyBlue==lotteryBlue)){
			System.out.println("五等奖");
			getMoney+=10;
		}
		else if(buyBlue==lotteryBlue){
			System.out.println("六等奖");
			getMoney+=5;
		}
	}
	public void welcome(){
		System.out.println("*****欢迎进入双色球彩票系统*****");
		System.out.println("       1.购买彩票");
		System.out.println("       2.查看开奖");
		System.out.println("       3.退出");
		System.out.println("*************************");
		System.out.print("选择菜单:");
	}
	public void buyLottery(){
		buyRed = new int[6];
		System.out.print("请输入你的购彩号码,以空格隔开:");
		Scanner in =new Scanner(System.in);
		for(int i=0;i<6;i++){
			buyRed[i]=in.nextInt();
		}
		buyBlue = in.nextInt();
		Arrays.sort(buyRed);
		buyMoney+=2;
	}
	public void getLottery(){
		if(buyRed!=null||buyBlue!=0){
			lotteryRed = new int[6];
			Random r = new Random();
			System.out.print("购彩号码为:");
			for(int i=0;i<6;i++){
				System.out.print(buyRed[i]+" ");
				lotteryRed[i]=r.nextInt(33)+1;
			}
			System.out.println(buyBlue);
			lotteryBlue = r.nextInt(16)+1;
			Arrays.sort(lotteryRed);
			int redCount=0;
			System.out.print("开奖号码为:");
			for(int i=0;i<6;i++)
			{
				System.out.print(lotteryRed[i]+" ");
				if(buyRed[i]==lotteryRed[i]){
					redCount++;
				}
			}
			System.out.println(lotteryBlue);
			countMoney(redCount);
			System.out.println("你一共下注"+buyMoney+"元"+"你一共中奖"+getMoney+"元");
			buyRed=null;
			buyBlue=0;
		}
		else
		{
			System.out.println("没有购买彩票不能开奖");
		}
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是福利彩票双色球Java的实现: ```java import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class DoubleColorBall { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入您要购买的彩票注数:"); int n = input.nextInt(); int[][] tickets = new int[n][7]; for (int i = 0; i < n; i++) { System.out.println("请输入第" + (i + 1) + "注彩票的号码:"); for (int j = 0; j < 6; j++) { System.out.print("请输入第" + (j + 1) + "个红球号码(1-33):"); int redBall = input.nextInt(); while (redBall < 1 || redBall > 33) { System.out.print("输入有误,请重新输入第" + (j + 1) + "个红球号码(1-33):"); redBall = input.nextInt(); } tickets[i][j] = redBall; } System.out.print("请输入蓝球号码(1-16):"); int blueBall = input.nextInt(); while (blueBall < 1 || blueBall > 16) { System.out.print("输入有误,请重新输入蓝球号码(1-16):"); blueBall = input.nextInt(); } tickets[i][6] = blueBall; } System.out.println("您购买的彩票号码为:"); for (int i = 0; i < n; i++) { System.out.print("第" + (i + 1) + "注:"); for (int j = 0; j < 6; j++) { System.out.print(tickets[i][j] + " "); } System.out.println("蓝球:" + tickets[i][6]); } int[] result = getResult(); System.out.println("本期开奖号码为:" + Arrays.toString(Arrays.copyOf(result, 6)) + " 蓝球:" + result[6]); int totalMoney = n * 2; int totalPrize = 0; for (int i = 0; i < n; i++) { int prize = getPrize(tickets[i], result); totalPrize += prize; if (prize > 0) { System.out.println("第" + (i + 1) + "注彩票中奖了,中奖金额为:" + prize + "元"); } } System.out.println("您一共下注" + totalMoney + "元,累计中奖" + totalPrize + "元"); } public static int[] getResult() { int[] result = new int[7]; Random random = new Random(); for (int i = 0; i < 6; i++) { int redBall = random.nextInt(33) + 1; while (contains(result, redBall, i)) { redBall = random.nextInt(33) + 1; } result[i] = redBall; } Arrays.sort(result, 0, 6); int blueBall = random.nextInt(16) + 1; result[6] = blueBall; return result; } public static boolean contains(int[] array, int value, int length) { for (int i = 0; i < length; i++) { if (array[i] == value) { return true; } } return false; } public static int getPrize(int[] ticket, int[] result) { int redCount = 0; int blueCount = 0; for (int i = 0; i < 6; i++) { if (contains(result, ticket[i], 6)) { redCount++; } } if (ticket[6] == result[6]) { blueCount = 1; } if (redCount == 6 && blueCount == 1) { return 10000000; } else if (redCount == 6) { return 500000; } else if (redCount == 5 && blueCount == 1) { return 3000; } else if (redCount == 5 || (redCount == 4 && blueCount == 1)) { return 200; } else if (redCount == 4 || (redCount == 3 && blueCount == 1)) { return 10; } else if (blueCount == 1) { return 5; } else { return 0; } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值