Java模拟双色球

学习完Java基础,我们可以尝试做下面这一个题目:

我们可以将这个问题分成三个部分: 

1.系统随机生成双色球号(6给红色球,1个蓝色球)

2.用户输入所选的双色球号码

3.比较用户与系统的双色球号,得出中奖金额

一、系统随机生成双色球号

         随机生成号码我们可以存在一个整形数组中 

//系统随机生成双色球号 红6个,蓝1个
    public static int[] create() {
        int[] creat = new int[7];
        Random random = new Random();
        for (int i = 0; i < 6; i++) {

            creat[i] = random.nextInt(33) + 1;
        }
        creat[6] = random.nextInt(7) + 1;
        return creat;
    }

二、用户输入双色球号

        1.首先也是先创建一个input整形数组用来存储用户输入的双色球号

        2.定义红色球号输入的值为a,判断a是否满足1-33,是否重复,未重复则按顺序存储在input数组中,蓝色球也如此,先定义输入值为判断是否满足条件再存储在input[6]中

//用户输入双色球号
    public static int[] input() {
        Scanner sc = new Scanner(System.in);
        int[] input = new int[7];
        int a = 0;
        int i = 0;
        while (i < 6) {
            System.out.println("请输入第" + (i + 1) + "红色球号:");
            a = sc.nextInt();
            if (a < 1 || a > 33) {
                System.out.println("你输入的数字超出范围,请重新输入:");
            } else {
                boolean repeat = false;
                for (int j = 0; j < i; j++) {
                    if (input[j] == a) {
                        repeat = true;
                        break;
                    }
                }
                if (!repeat) {
                    input[i++] = a;
                } else {
                    System.out.println("你输入的号码已重复,请重新输入");
                }
            }
        }
        System.out.println("请输入蓝色球号:");
        int b = 0;
        while (true) {
            b = sc.nextInt();
            if (b > 0 && b < 8) {
                input[6] = b;
                break;
            } else {
                System.out.println("请输入的蓝色球号范围在1-7:");
            }
        }
        return input;
    }

三、比较用户与系统的双色球号

1.接收系统随机生成的双色球号跟用户输入的双色球号

2.根据中奖条件输出中奖金额

//比较
    public static void result(int[] create, int[] input) {
        int n = 0;
        int m = 0;
        System.out.println("本次中奖号码是:" + Arrays.toString(create));
        System.out.println("你输入的号码是:" + Arrays.toString(input));
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (input[i] == create[j]) {
                    n++;
                }
            }
        }
        if (create[6] == input[6]) {
            m = 1;
        }
        if (m == 1) {
            switch (n) {
                case 0, 1, 2:
                    System.out.println("恭喜你中奖五元");
                    break;
                case 3:
                    System.out.println("恭喜你中奖十元");
                    break;
                case 4:
                    System.out.println("恭喜你中奖两百元");
                    break;
                case 5:
                    System.out.println("恭喜你中奖3000元");
                    break;
                case 6:
                    System.out.println("恭喜你中奖1000万元");
                    break;
                default:
            }
        } else {
            switch (n) {
                case 4:
                    System.out.println("恭喜你中奖10元");
                    break;
                case 5:
                    System.out.println("恭喜你中奖200元");
                    break;
                case 6:
                    System.out.println("恭喜你中奖500万元");
                    break;
                default:
            }
        }
    }
}

四、下面是完整代码展示

package com.itheima.test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Shuangseqiu {
    public static void main(String[] args) {
        result(create(), input());
    }

    //系统随机生成双色球号 红6个,蓝1个
    public static int[] create() {
        int[] creat = new int[7];
        Random random = new Random();
        for (int i = 0; i < 6; i++) {

            creat[i] = random.nextInt(33) + 1;
        }
        creat[6] = random.nextInt(7) + 1;
        return creat;
    }


    //用户输入双色球号
    public static int[] input() {
        Scanner sc = new Scanner(System.in);
        int[] input = new int[7];
        int a = 0;
        int i = 0;
        while (i < 6) {
            System.out.println("请输入第" + (i + 1) + "红色球号:");
            a = sc.nextInt();
            if (a < 1 || a > 33) {
                System.out.println("你输入的数字超出范围,请重新输入:");
            } else {
                boolean repeat = false;
                for (int j = 0; j < i; j++) {
                    if (input[j] == a) {
                        repeat = true;
                        break;
                    }
                }
                if (!repeat) {
                    input[i++] = a;
                } else {
                    System.out.println("你输入的号码已重复,请重新输入");
                }
            }
        }
        System.out.println("请输入蓝色球号:");
        int b = 0;
        while (true) {
            b = sc.nextInt();
            if (b > 0 && b < 8) {
                input[6] = b;
                break;
            } else {
                System.out.println("请输入的蓝色球号范围在1-7:");
            }
        }
        return input;
    }


    //比较
    public static void result(int[] create, int[] input) {
        int n = 0;
        int m = 0;
        System.out.println("本次中奖号码是:" + Arrays.toString(create));
        System.out.println("你输入的号码是:" + Arrays.toString(input));
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (input[i] == create[j]) {
                    n++;
                }
            }
        }
        if (create[6] == input[6]) {
            m = 1;
        }
        if (m == 1) {
            switch (n) {
                case 0, 1, 2:
                    System.out.println("恭喜你中奖五元");
                    break;
                case 3:
                    System.out.println("恭喜你中奖十元");
                    break;
                case 4:
                    System.out.println("恭喜你中奖两百元");
                    break;
                case 5:
                    System.out.println("恭喜你中奖3000元");
                    break;
                case 6:
                    System.out.println("恭喜你中奖1000万元");
                    break;
                default:
            }
        } else {
            switch (n) {
                case 4:
                    System.out.println("恭喜你中奖10元");
                    break;
                case 5:
                    System.out.println("恭喜你中奖200元");
                    break;
                case 6:
                    System.out.println("恭喜你中奖500万元");
                    break;
                default:
            }
        }
    }
}

五、测试结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值