A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© longlong1990 中级黑马   /  2016-11-24 22:48  /  973 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一道综合性比较强的基础练习题:
        模拟彩票:
        随机生成6个0-15的数字,再从键盘录入6个0-15的数字,
        当随机生成的数字和键盘录入的数字:
        3个一样,中三等奖
        4个一样,中二等奖
        5个一样,中一等奖
        全部一样,中特等奖
        最终打印样式如下:
        "您的号码为XX,XX,XX,XX,XX,XX,中了X等奖,恭喜您!"或者
        "您的号码为XX,XX,XX,XX,XX,XX,没有中奖,!"
        public class Test {
                public static void main(String[] args) {
                        // 随机系统和键盘录入系统
                        Scanner sc = new Scanner(System.in);
                        Random ran = new Random();
                        // 创建一个集合,用来存储随机到的彩票数字
                        ArrayList<Integer> ranList = new ArrayList<Integer>();
                        // 使用for循环添加6个0-15的数字,判断是否重复,如果重复则不添加
                        for (int i = 0;; i++) {
                                int number = ran.nextInt(16);
                                if (!ranList.contains(number)) {
                                        ranList.add(number);
                                }
                                // 判断集合长度,得到长度为6的集合
                                if (ranList.size() > 6) {
                                        break;
                                }
                        }
                        // 创建一个集合,用来存储键盘录入的数字
                        ArrayList<Integer> scList = new ArrayList<Integer>();
                        for (int i = 0; i < 6; i++) {
                                System.out.println("请输入第" + (i + 1) + "个数字:");
                                scList.add(sc.nextInt());
                                // 这里同样进行判断,得到长度为6的集合
                                if (scList.size() > 6) {
                                        break;
                                }
                        }
                        // 定义变量,用来记录随机彩票数字和录入数字相同的个数
                        int count = 0;
                        // 使用嵌套for循环,对两个集合中的元素一一比较,记录相同数字的个数
                        for (int i = 0; i < ranList.size(); i++) {
                                for (int j = 0; j < scList.size(); j++) {
                                        if (ranList.get(i).equals(scList.get(j))) {
                                                count++;
                                        }
                                }
                        }
                        // 对集合进行了排序,主要是看着顺眼一些。。
                        //Collections.sort(ranList);
                        //Collections.sort(scList);
                       
                        // 根据打印要求进行输出打印
                        System.out.print("中奖的号码为");
                        for (int i = 0; i < ranList.size(); i++) {
                                System.out.print(ranList.get(i) + ",");
                        }
                        System.out.println();
                        System.out.print("您的号码为");
                        for (int i = 0; i < scList.size(); i++) {
                                System.out.print(scList.get(i) + ",");
                        }
                        System.out.println();
                        // 根据相同数字的个数,进行判断是否中奖
                        if (count == 3) {
                                System.out.println("中了三等奖,恭喜您!");
                        } else if (count == 4) {
                                System.out.println("中了二等奖,恭喜您!");
                        } else if (count == 5) {
                                System.out.println("中了一等奖,恭喜您!");
                        } else if (count == 6) {
                                System.out.println("中了特等奖,恭喜您!");
                        } else {
                                System.out.println("没有中奖!");
                        }
                }
        }

您需要登录后才可以回帖 登录 | 加入黑马