JAVA案例:实现双色球

 

package com.itheima.anli;

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



public class Anli5 {
    public static void main(String[] args) {
        int[] user=user();
        printarr(user);
        System.out.println();
        int[] xitong=xitong();
        printarr(xitong);
        System.out.println();
        pan(user,xitong);

    }
    public static void pan(int[] user,int[] xitong){
        int num=0;
        int num1=0;
        for (int i = 0; i < 6; i++) {

            if(user[i]==xitong[i]){
                num++;
            }
        }
        if(user[6]==xitong[6]){
            num1++;
        }
        System.out.println("中了"+num+"红球;"+" "+"中了"+num1+"蓝球;");
    }

    public static int[] user(){
        Scanner scanner=new Scanner(System.in);
        int[] zong=new int[7];
        for (int i = 0; i <6; i++) {
            while (true) {
                System.out.println("请您输入第"+(i+1)+"个红球号码(1-33不能重复)");
                int a=scanner.nextInt();
                if(a<1||a>33){
                    System.out.println("请您输入的红球号码1-33之间的数");
                }
                else {
                   if(exit(a,zong)){
                       System.out.println("请您输入的红球号码不能重复");
                   }
                   else {
                       zong[i]=a;
                       break;
                   }
                }
            }
        }
        while (true) {
            System.out.println("请您输入蓝球号码(1-16)");
            int b=scanner.nextInt();
            if(b<1||b>16){
                System.out.println("请您输入蓝球号码值在1-16之间");
            }
            else {
                zong[6]=b;
                break;
            }
        }
        return zong;
    }

    public static int[] xitong(){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        int[] zong=new int[7];
        for (int i = 0; i <6; i++) {
            while (true) {
                int a=random.nextInt(33)+1;
                    if(!exit(a,zong)){
                        zong[i]=a;
                        break;
                    }

            }
        }
        while (true) {
            int b=random.nextInt(16)+1;
            zong[6]=b;
            break;

        }
        return zong;
    }



    private static void printarr(int[] shu){
        System.out.print("[");
        for (int i = 0; i < shu.length; i++) {
            System.out.print(i==shu.length-1?shu[i]:shu[i]+",");
        }
        System.out.print("]");
    }

    private static boolean exit(int a, int[] zong) {
        for (int j = 0; j <zong.length; j++) {
            if(a==zong[j]){
                return true;
            }
        }
        return false;
    }
}

 

posted @ 2024-03-03 21:09  小彭先森  阅读(7)  评论(0编辑  收藏  举报