java入门之双色球

public class RedandBlue {
    public static void main(String[] args) {
        //双色球 (红球:1~33 号码不重复)(蓝球:1~16);
        int[] code1 = input();
        System.out.println("用户输入的双色球号码:");
        printarray(code1);
        System.out.println();
        int[] code2 = lucknumber();
        System.out.println("中奖的的双色球号码:");
        printarray(code2);
        judge(code1,code2);
    }
    //打印数组的方法;
    public static void printarray(int[] a)
    {
        System.out.print("[");
        for (int i = 0; i < a.length; i++) {
            System.out.print(i == a.length-1?a[i]:a[i] + ",");
        }
        System.out.println("]");
    }
    //1.让用户输入7个号码,并返回;(6+1)
    public static int[] input() {//方法无参数,直接提示用户输入;
        int[] code1 = new int[7];
        Scanner sc = new Scanner(System.in);
            for (int i = 0; i < code1.length - 1; i++) {
                while (true) {
                System.out.print("请输入第" + (i + 1) + "个红球号码:");
                System.out.println("(提示:红球的号码范围是:1~33)");
                int number = sc.nextInt();
                if(number<1||number>33)
                    System.out.println("您的输入有误,不在允许范围内!");
                else //在范围内,继续检查是否重复;
                {
                    if(exit(code1,number))
                        System.out.println("您有重复操作,请重新输入!");
                    else //在范围,且不重复;
                    {
                        code1[i] = number;
                        break;
                    }
                }
            }
        }
            //第一次写错是因为:把蓝球这部分代码放在了for循环体系内;
        System.out.println("请输入蓝球的号码:");
            while(true) {
                int number = sc.nextInt();//蓝球;
                if (number < 1 || number > 16)
                    System.out.println("您的输入有误,不在蓝球号码范围内!请重新输入:");
                else
                {
                    code1[code1.length - 1] = number;
                    break;
                }
            }
        return code1;
    }

    private static boolean exit(int[] code,int number) {
        //需求:检查number是否在code中;
        for (int i = 0; i < code.length; i++) {
            if (number == code[i])
                return true;
        }
        return false;
    }

    //2.随机生成7个号码,并返回;(6+1)
    public static int[] lucknumber()
    {//方法无参数,直接随机生成7位数字;//前六位也不能存在重复元素;
        Random r = new Random();
        int[] code2 = new int[7];
        for (int i = 0; i < code2.length - 1; i++) {
            while (true) {
                int number = r.nextInt(33) + 1;
                if(!exit(code2,number))//如果之前不重复;
                    code2[i] = number;
                    break;
            }
        }
        //蓝球 :
        code2[code2.length-1] = r.nextInt(16) + 1;

        return code2;
    }
    //3.判断中奖情况,比较两个数组;
    public static void judge(int[] a,int[] b)
    {//需求 :统计红球命中个数,以及蓝球是否命中;
        int red = 0;
        int blue = 0;
        //红球;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if(a[i]==b[j])
                    red++;
            }
        }
        //蓝球;
        if(a[6]==b[6])
            blue = 1;
        //根据红蓝球个数,判断中奖情况;
        if(red==6&&blue==1)
            System.out.println("恭喜你中奖" + "1000万!");
        else if (red==6&&blue==0)
            System.out.println("恭喜你中奖" + "500万!");
        else if (red==5&&blue==1)
            System.out.println("恭喜你中奖" + "300元!");
        else if (red==5&&blue==0)
            System.out.println("恭喜你中奖" + "200元!");
        else if (red==4&&blue==1)
            System.out.println("恭喜你中奖" + "200元!");
        else if (red==4&&blue==0)
            System.out.println("恭喜你中奖" + "10元!");
        else if (red==3&&blue==1)
            System.out.println("恭喜你中奖" + "10元!");
        else if (red==2&&blue==1)
            System.out.println("恭喜你中奖" + "10元!");
        else if (red==1&&blue==1)
            System.out.println("恭喜你中奖" + "5元!");
        else if (red==0&&blue==1)
            System.out.println("恭喜你中奖" + "5元!");
        else
            System.out.println("抱歉,下次好运哦!~");
    }
}

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