练习

原创
2019/11/08 19:43
阅读数 95

双色球——模拟彩票系统

1.用户可以选择出票模式(1 机选   2 手选)             
2.如果选择的是机选,则随机生成六个红球数字和一个蓝球数字,
 说明六个红球数字的范围是1-33;蓝球一个,数字范围是1-16
3.如果是机选 用户还可以选择注数,如果选择的是2注,则随机生成两组双色球
4.同时程序还得随机生成中奖的双色球 号码。
5.用户的双色球和生成的中奖号码,进行对比,并提示有没有中奖,
中奖规则是:
一等奖:6+1
二等奖:6+0
三等奖:5+1
四等奖:5+0/4+1
五等奖:4+0/3+1
六等奖:2+1/1+1/0+1
6.如果是手选,则先收到输入六个红球数字 ,再输入篮球数字,输
入完成则计算中奖结果并显示。

package com.day11;

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

/**
 * @author SFJ
 * @date 2019/11/8
 * @time 15:24
 **/
public class lotteryTicket {
    static Random random = new Random();
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("------------------双色球彩票系统--------------------");
        System.out.println("----------1.机选------------------2.手选-----------");
        int select = scanner.nextInt();
        switch (select)
        {
            case 1:
            {
                System.out.println("请输入注数");
                int machinenum = scanner.nextInt();
                machine(machinenum);
                break;
            }
            case 2:
            {
                System.out.println("请输入注数");
                int personnum = scanner.nextInt();
                person(personnum);
                break;
            }
        }

    }
    public static  int[] randomPrize()
    {
        int prize[]=new int[7];
        for (int i=0;i<6;i++)
        {
            prize[i]=random.nextInt(33)+1;
            for (int j=0;j<i;j++)
            {
                if (prize[j]==prize[i])
                {
                    prize[i]=random.nextInt(33)+1;
                }
            }
        }
        prize[6]=random.nextInt(16)+1;
        Arrays.sort(prize,0,5);
        System.out.print("生成的中奖红色球为:");
        for (int i=0;i<6;i++)
        {
            System.out.print(prize[i]+" ");
        }

        System.out.print("中奖蓝色球:"+prize[6]+"\n");
        return prize;
    }
    public static void machine(int n)
    {
        int machine[][]= new int[n][7];
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<6;j++)
            {
                machine[i][j]= (random.nextInt(33))+1;
                for (int m=0;m<j;m++)
                {
                    if (machine[i][j]==machine[i][m])
                    {
                        machine[i][j]=(random.nextInt(33))+1;
                    }
                }
                machine[i][6]=(random.nextInt(16))+1;
            }

        }
        for (int i=0;i<n;i++)
        {
            System.out.print("第"+(i+1)+"次"+"机器生成的红色球的号码是");
            Arrays.sort(machine[i],0,5);
            for (int j=0;j<6;j++)
            {
                System.out.print(machine[i][j]+" ");
            }
            System.out.print("蓝色球是:"+machine[i][6]+"\n");
        }
        result(machine,n);
    }
    public static void person(int n)
    {
        int person[][]=new int[n][7];
        for (int i=0;i<n;i++)
        {
            System.out.println("请输入6个不同的1-33红色球数字:");
            for (int j=0;j<6;j++)
            {
               int t=scanner.nextInt();
               person[i][j]=t;
               for (int q=0;q<j;q++)
                   {
                       if(person[i][q]==t)
                       {
                           while (true)
                           {
                               System.out.println("不能出现相同的数字"+person[i][q]+",请重新输入:");
                               person[i][j]=scanner.nextInt();
                               if (person[i][q]!=person[i][j])
                                   break;
                           }


                       }
                   }




               while(true)
               {
                   if (person[i][j]>33||person[i][j]<1)
                   {
                       System.out.println("请重新输入一个值,数字范围在1~33之间");
                       person[i][j]=scanner.nextInt();
                   }
                   else
                   {
                       break;
                   }
               }

            }
            System.out.println("请输入所选的蓝球数字:");
            person[i][6]=scanner.nextInt();
            if (person[i][6]>16||person[i][6]<1)
            {
                System.out.println("请重新输入一个值,数字范围在1~16之间");
                person[i][6]=scanner.nextInt();
            }

        }
        System.out.println("输入的红球数字为:");
        for (int i=0;i<n;i++)
        {
            Arrays.sort(person[i],0,5);
            for (int j=0;j<6;j++)
            {
                System.out.print(person[i][j]+"  ");
            }
            System.out.println("输入的蓝球为"+person[i][6]+"\n");
        }
        result(person,n);
    }
    public static void result(int r[][],int row) {
        int redsum = 0;
        int bluesum = 0;
        int rowredsum[] = new int[row];
        int rowbluesum[] = new int[row];
        int prize[] = randomPrize();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < 6; j++) {
                for (int p = 0; p < 6; p++) {
                    if (prize[p] == r[i][j]) {
                        redsum++;
                    }
                }
            }
            rowredsum[i] = redsum;
            redsum = 0;
            if (prize[6] == r[i][6]) {
                bluesum++;
            }
            rowbluesum[i] = bluesum;
            bluesum = 0;
        }
        for (int i = 0; i < row; i++) {
            if (rowredsum[i] == 6 && rowbluesum[i] == 1) {
                System.out.println("第" +(i+1) + "次中一等奖");
            }
            else if (rowredsum[i] == 6 && rowbluesum[i] == 0) {
                System.out.println("第" + (i+1) + "次中2等奖");
            }
            else if (rowredsum[i] == 5 && rowbluesum[i] == 1) {
                System.out.println("第" + (i+1) + "次中3等奖");
            }
            else if ((rowredsum[i]==5&&rowbluesum[i]==0)||(rowredsum[i]==4&&rowbluesum[i]==1))
            {
                System.out.println("第"+(i+1)+"次中4等奖");
            }
            else if ((rowredsum[i]==4&&rowbluesum[i]==0)||(rowredsum[i]==3&&rowbluesum[i]==1))
            {
                System.out.println("第" + (i+1) + "次中5等奖");
            }
            else if ((rowredsum[i]==2&&rowbluesum[i]==1)||(rowredsum[i]==1&&rowbluesum[i]==1)||(rowredsum[i]==0&&rowbluesum[i]==1))
            {
                System.out.println("第" + (i+1) + "次中6等奖");
            }
            else
            {
                System.out.println("第" + (i+1) +"没有中奖");
            }
        }
    }




}

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部