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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wx_pmVIrag6 中级黑马   /  2016-7-27 22:54  /  413 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Scanner;

class Demo6 {
        public static void main(String[] args) {
                int[] red = new int[6];
                int blu;
                Demo6 sc = new Demo6();
               
                //红色球中奖号码       
                for (int i = 0; i < red.length; i++) {
                        red[i] = (int)(Math.random()*33) +1;
                }
               
                //蓝色球中奖号码
                blu = (int)(Math.random()*16)+1;
               
                //客户选取号码
                int[] myred = sc.Red();
                int myblu = sc.Blu();
               
                //客户红色球中奖个数
                int redcount = sc.zhongRed(red, myred);

                //客户蓝色球中奖个数
                int blucount = sc.zhongBlu(blu, myblu);
               
                //客户是否中奖
                sc.guizhe(redcount, blucount);
               
                System.out.print("红色球中奖号码为:");
                for (int i = 0; i < red.length; i++) {
                        System.out.print(red[i] + "\t");
                }
                System.out.println();
                System.out.println("蓝色球中奖号码为:" + blu);
               
                System.out.print("您选取的红色号码为:");
                for (int i = 0; i < myred.length; i++) {
                        System.out.print(myred[i] + "\t");
                }
                System.out.println();
                System.out.println("您选取的蓝色号码为:" + myblu);

        }
       
        //客户选取红色球
        public static int[] Red(){
                Scanner sc = new Scanner(System.in);
                int[] red= new int[6];

                System.out.println("请您选取红色球:");
                for (int i = 0; i < red.length; i++) {
                        red[i] = sc.nextInt();
                        while(true){
                                if ((red[i] > 33)||(red[i] < 1)) {
                                        System.out.println("您输入的数字超出范围,请重新输入:");
                                        red[i] = sc.nextInt();
                                }else{
                                        break;
                                }
                        }
                        for (int j = 0; j <=(i-1); j++) {
                                if (red[i] == red[j]) {
                                        System.out.println("您已经选取的此数据,请重新选择!");
                                        i--;
                                }
                        }
                }

                return red;       
        }

        //客户选取蓝色球
        public static int Blu(){
                Scanner sc = new Scanner(System.in);
                System.out.println("请您选取蓝色球:");
                int blu = sc.nextInt();
                while(true){
                        if ((blu > 16)||(blu < 1)) {
                                System.out.println("您输入的数字超出范围,请重新输入:");
                                blu = sc.nextInt();
                        }else{
                                break;
                        }
                }
                return blu;
        }

        //客户红色球中了个数
        public static int zhongRed(int[] red, int[] myred){
                int count = 0;
                for (int i = 0; i < red.length; i++) {
                        if (red[i] == myred[i]) {
                                count++;
                        }
                }
               
                return count;
        }

        //客户蓝色球是否中了
        public static int zhongBlu(int blu, int myblu){
                int count=0;
                if (blu == myblu) {
                        count++;
                }
                return count;
        }

        //客户中奖情况
        public static void guizhe(int red, int blu){
                if (blu == 1) {
                        if (red == 6) {
                                System.out.println("一等奖");
                        }
                        else if (red == 5) {
                                System.out.println("二等奖");
                        }
                        else if (red == 4) {
                                System.out.println("三等奖");
                        }
                        else if (red == 3) {
                                System.out.println("安慰奖");
                        }
                }else{
                        System.out.println("没中奖");
                }
        }

}

2 个回复

倒序浏览
顶起,顶起,顶起
回复 使用道具 举报
会玩会玩
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马