JAVA模拟双色球

15 篇文章 0 订阅
2 篇文章 0 订阅
import java.util.Random;
import java.util.Scanner;

public class Main {
    static Random random = new Random();
    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int[] winCode = new int[7];
        int[] userCode = new int[7];
        int countBlue = 0, countRed = 0;
        winCode(winCode);
        System.out.println("中奖号码为:");
        for (int i = 0; i < winCode.length; i++) {
            System.out.print(winCode[i] + " ");
        }
        System.out.println();
        for (int i = 0; i < userCode.length - 1; i++) {
            System.out.println("请输入第" + (i + 1) + "位红球");
            int num = scanner.nextInt();
            while (num > 33 || num < 1) {
                System.out.println("请输入1-33之间的数");
                num = scanner.nextInt();
            }
            userCode[i] = num;
        }
        System.out.println("请输入蓝球");
        int num = scanner.nextInt();
        while (num > 16 || num < 1) {
            System.out.println("请输入1-16之间的数");
            num = scanner.nextInt();
        }
        userCode[6] = num;
        // 判断中奖情况
        for (int i = 0; i < winCode.length - 1; i++) {
            for (int j = 0; j < userCode.length - 1; j++) {
                if (winCode[j] == userCode[i]) {
                    countRed++;
                }
            }
        }
        if (winCode[6] == userCode[6]) {
            countBlue++;
        }
        System.out.println("你的号码为:");
        for (int i = 0; i < userCode.length; i++) {
            System.out.print(userCode[i] + " ");
        }
        System.out.println();
        if ((countRed == 0 && countBlue == 1) || (countRed == 1 && countBlue == 1) || (countRed == 2 && countBlue == 1)) {
            System.out.println("感谢您中了六等奖");
        } else if ((countRed == 3 && countBlue == 0) || (countRed == 4 && countBlue == 0)) {
            System.out.println("恭喜您中了五等奖");
        } else if ((countRed == 4 && countBlue == 1) || (countRed == 5 && countBlue == 0)) {
            System.out.println("恭喜您中了四等奖");
        } else if (countRed == 5 && countBlue == 1) {
            System.out.println("恭喜您中了三等奖");
        } else if ((countRed == 6 && countBlue == 0)) {
            System.out.println("恭喜您中了二等奖");
        } else if (countRed == 6 && countBlue == 1) {
            System.out.println("恭喜您中了一等奖");
        } else {
            System.out.println("很遗憾,您没有中奖");
        }

    }

    public static void winCode(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            a[i] = random.nextInt(33) + 1;
            for (int j = 0; j < i; j++) {
                if (a[i] == a[j]) {
                    i--;
                    break;
                }
            }
        }
        a[a.length - 1] = random.nextInt(16) + 1;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值