Java实现双色球

 
  
package com.hyb;

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

/**
 * @Author: Heyibo
 * @Date: 2023/7/15 20:39
 */
public class Shuangseqiu {
    public static void main(String[] args) {
        int[] userNumbers= userSelectNumber();
        System.out.println("您投注的号码为:");
        printArray(userNumbers);

        int[] luckNumbers= creatLuckNumber();
        System.out.println("中奖号码为:");
        printArray(luckNumbers);

        judge(userNumbers,luckNumbers);

    }
    /*设计一个方法,让用户投入六个红球和一个蓝球号码,并展示出来*/
    //遍历数组
    public static void printArray(int arr[]){
        System.out.print("红球号码为[");
        for (int i = 0; i < arr.length-1; i++) {
            System.out.print(i==arr.length-2 ? arr[i]:arr[i]+",");
        }
        System.out.print("]"+"\t");
        System.out.println("蓝球号码为["+arr[arr.length-1]+"]");

    }
    //用户输入双色球号码
    public static int[] userSelectNumber(){
        int[] numbers=new int[7];

        Scanner sc=new Scanner(System.in);

        for (int i = 0; i < numbers.length-1; i++) {
            while (true) {
                System.out.println("请您输入第"+(i+1)+"个红球号码(1到33之间不能重复)");
                int number=sc.nextInt();

                if (number<1 || number>33){
                    System.out.println("对不起,您输入的号码不在1~33之间,请重新输入");
                }else {
                    if (exist(numbers,number)){
                        System.out.println("对不起,您输入的数字有重复,请确认!!");
                    }else {
                        numbers[i]=number;
                        break;
                    }
                }
            }
        }
        while (true) {
            System.out.println("请输入蓝球号码(1到16之间)");
            int number=sc.nextInt();
            if (number<1||number>16){
                System.out.println("对不起,您输入的号码不在1~16之间,请重新输入");
            }else {
                numbers[6]=number;
                break;
            }
        }
        return numbers;
    }
    //判断是否重复
    private static boolean exist(int[] numbers, int number) {
        for (int i = 0; i < numbers.length-1; i++) {
            if (numbers[i]==0){
                break;
            }
            if (numbers[i]==number){
                return true;
            }
        }
        return false;
    }

    /*设计一个方法,随机一组幸运号码出来六个红球一个蓝球*/
    public static int[] creatLuckNumber(){
        int[] numbers=new int[7];

        Random r=new Random();
        for (int i = 0; i < numbers.length-1; i++) {
            while (true) {
                int number= r.nextInt(33)+1;
                if (!exist(numbers,number)){
                    numbers[i]=number;
                    break;
                }
            }
        }
        numbers[6]=r.nextInt(16)+1;
        return numbers;
    }

    /*设计一个方法,判断用户的中奖情况*/
    public static void judge(int[] userNumbers,int[] luckNumbers){

        int redCount=0;
        int blueCount=0;
        //判断红球命中数量
        for (int i = 0; i < userNumbers.length-1; i++) {
            for (int j = 0; j < luckNumbers.length-1; j++) {
                if (userNumbers[i]==luckNumbers[j]){
                    redCount++;
                    break;
                }
            }
        }
        //判断蓝球命中数量
        blueCount=userNumbers[6]==luckNumbers[6] ? 1:0;
        System.out.println("您命中的红球数量为:"+redCount);
        System.out.println("您命中的蓝球数量为:"+blueCount);

        //判断中奖情况
        if (redCount==6 && blueCount==1){
            System.out.println("恭喜您,中大奖1000万,可以开始享受人生了~~~");
        }else if (redCount==6 && blueCount==0){
            System.out.println("恭喜您,中奖500万,可以稍微享受人生了~~~");
        }else if (redCount==5 && blueCount==1) {
            System.out.println("恭喜您,中奖3000元,可以出去旅游了~~~");
        }else if (redCount==5 && blueCount==0 || redCount==4 && blueCount==1) {
            System.out.println("恭喜您,中小奖200元,可以出去吃一顿~~~");
        }else if (redCount==4 && blueCount==0 || redCount==3 && blueCount==1) {
            System.out.println("恭喜您,中奖10元~~~");
        }else if (redCount<3 && blueCount==1){
            System.out.println("恭喜您中奖5元");
        }else {
            System.out.println("感谢您为福利事业做出巨大贡献~~");
        }
    }
}
}

你可能感兴趣的:(java,算法,开发语言)