项目彩票系统

 彩票系统
    功能模块
        菜单选择
        自选号码
        机选号码
        模拟开奖
        兑奖
        退出
    核心技术
        运算符
        流程控制
            顺序执行
            选择执行
                if
                switch
            循环执行
                while
                for
                foreach
        数组
            数组定义
            数组赋值和取值
            排序算法
                选择排序
                冒泡排序
                快速排序
        方法
            方法定义
            方法调用
            方法传参和返回值
        格式化数字
            逻辑判断拼接
            延伸数字格式化类java.text.DecimalFormat

IDEA控制台
    功能菜单选择
        自选号码
            输入彩票号
                数字检验
                号码范围检验
                号码重复检验
            彩票号码排序
                选择排序
                冒泡排序
                快速排序
            彩票号码格式化
                逻辑拼接
                格式化类
        机选号码
            随机彩票号
                随机指定范围号码
                随机号码重复检验
        模拟开奖
            新的机选号码
        兑奖
            计算中奖等级
            根据获奖等级输出奖金
        退出

核心技术
    运算符
    流程控制
        顺序执行
        选择执行
            if
            switch
        循环执行
            while
            for
            foreach
    数组
        数组定义
        数组赋值和取值
        排序算法
            选择排序
            冒泡排序
            快速排序
    方法
        方法定义
        方法调用
        方法传参和返回值
    格式化数字
        逻辑判断拼接
        延伸数字格式化类java.text.DecimalFormat

彩票系统
红区:取值范围1–33 6个
蓝取:取值范围1–16 1个
一注彩票的号码不能有重复的
功能:
1.购买:手动输入彩票号码
判断是否中奖
2.随机:让电脑自动生成
可以手动选择随机多少注
3.退出
退出系统

下边请看我的源代码

package xx;

import java.text.DecimalFormat;
import java.util.Optional;
import java.util.Scanner;

public class Text01 {
    public static Integer[] cache;//缓存产生的彩票号码;
    public static String[] lottynum;//数字格式化;
    public static String[] pincNum;//随机生成开奖彩票号;
    public static int grade;//中奖的等级;
    public static double money;//中将的金额;

    public static void main(String[] args) {
        while (true) {
            System.out.println("");
            System.out.println("\t\t*************************");
            System.out.println("            1,自选\t\t\t 2,机选");
            System.out.println("            3,开奖\t\t\t 4,兑奖");
            System.out.println("            0,退出");

            System.out.println("请开始你的选择");
            int a = add();
            switch (a) {
                case 1:
                    Optional();
                    break;
                case 2:
                    machine();
                    break;
                case 3:
                    Drawaprize();
                    break;
                case 4:
                    cashaprize();
                    break;
                case 0:
                    System.exit(0);
                default:
                    System.out.println("后续功能有待完善");
                    break;
            }
        }
    }

    /**
     * 专门用来接受用户输入;
     *
     * @return
     */
    public static int add() {
        int key;
        try {
            key = new Scanner(System.in).nextInt();
        } catch (Exception e) { //捕获异常的语句;
            System.out.println("您输入的错误");
            key = add();
        }
        return key;
    }

    /**
     * 1号自选的方法;
     */
    public static void Optional() {
        System.out.println("   \t\t\t请选择双色球7个 ");
        System.out.println("    \t\t红球的范围是6个1—33");
        System.out.println("     \t\t蓝球的范围是1个1-16");
        cache = new Integer[7];
        for (int i = 0; i < 6; i++) {
            System.out.println("请输入" + (i + 1) + "个红球");
            cache[i] = add();
            if (!RedGlobe(i)) {
                i--;
            }
        }
        for (int i = 6; i < 7; i++) {
            System.out.println("请输入" + (i + 1) + "个蓝球");
            cache[i] = add();
            if (!blueball(i)) {
                i--;
            }
        }
        Bubbling();//冒泡排序;
        lottynum = new String[7];//把数组实体化在给它赋值;
        lottynumFormat(lottynum);//lottynum这个数组存放格式化后的数组;数组格式化的方法:
        show(lottynum);
    }

    /**
     * 验证红球的次数
     *
     * @return index数组的下表;
     */
    public static boolean RedGlobe(int index) {
        if (cache[index] < 1 || cache[index] > 33) {
            System.out.println("您输入的红球超出范围了");
            return false;
        }
        for (int i = 0; i < index; i++) {
            if (cache[i] == cache[index]) {
                System.out.println("红球不能重复");
                return false;

            }
        }
        return true;
    }

    /**
     * 验证篮球的次数;
     *
     * @param index
     * @return
     */
    public static boolean blueball(int index) {
        if (cache[index] < 1 || cache[index] > 16) {
            System.out.println("您输入的篮球超出范围了");
            return false;
        }

        return true;
    }

    /**
     * 2随机数;
     */
    public static void machine() {
        System.out.println("正在出号");
        cache = new Integer[7];
        randowNum();//随机生辰数字
        Bubbling();//冒泡排序调用;
        lottynum = new String[7];//把数组里的数准备格式化;
        lottynumFormat(lottynum);//数字格式化;
        show(lottynum);//输出;

    }

    /**
     * 3开奖;
     */
    public static void Drawaprize() {
        if (lottynum == null) {
            System.out.println("请先购买彩票");
            return;
        }
        cache = new Integer[7];
        randowNum();//这个时候产出的号码就是本期的中将号码;
        Bubbling();//冒牌排序;
        pincNum = new String[7];//随机生成开奖彩票号
        lottynumFormat(pincNum);//把随机随机从生成的开奖彩票号格式化;
        System.out.println("--------------------------");
        System.out.println("-------------本期中奖号码----");
        show(pincNum);//输出随机随机从生成的开奖彩票号
        System.out.println("-----------------------------");
        System.out.println("------------您购买彩票号码是------");
        show(lottynum);//输出自己购买的彩票号;
    }

    /**
     * 4兑奖的方法;
     */
    public static void cashaprize() {
        if (pincNum == null || lottynum == null) {
            System.out.println("请购买彩票或还没有开奖");
            return;
        }
        //你购买的彩票号码和开奖的彩票号码  红球相等的个数 篮球相等的个数;
        //先找红球;
        int redBall = 0;
        int blueBall = 0;
        for (int i = 0; i < pincNum.length - 1; i++) {
            for (int j = 0; i < lottynum.length - 1; i++) {
                if (pincNum[i].equals(lottynum[i])) {//开奖的红球等于购买彩票的红球看看有没有相等;
                    redBall++;//红球数量相加;
                    break;
                }
            }
        }
        //找篮球;
        if (pincNum[6].equals(lottynum[6])) {//奖的蓝球等于购买彩票的蓝球对比;
            blueBall++;
        }
        System.out.println("中了" + redBall + "个红球");
        System.out.println("中了" + blueBall + "个蓝球");
        if (redBall == 6 && blueBall == 1) {
            grade = 1;
        } else if (redBall == 6) {
            grade = 2;
        } else if (redBall == 5 && blueBall == 1) {
            grade = 3;
        } else if (redBall == 5 || redBall == 4 && blueBall == 1) {
            grade = 4;
        } else if (redBall == 4 || redBall == 3 && blueBall == 1) {
            grade = 5;
        } else if (redBall == 2 && blueBall == 1 || redBall == 1 && blueBall == 1 || blueBall == 1) {
            grade = 6;
        } else {
            grade = 0;
        }
        switch (grade) {
            case 1:
                money = 5000000;
                System.out.println("恭喜你中一等奖" + money);
                break;
            case 2:
                money = 50000;
                System.out.println("恭喜你中二奖" + money);
                break;
            case 3:
                money = 3000;
                System.out.println("恭喜你中三奖" + money);
                break;
            case 4:
                money = 200;
                System.out.println("恭喜你中奖" + money);
                break;
            case 5:
                money = 10;
                System.out.println("恭喜你中奖" + money);
                break;
            case 6:
                money = 5;
                System.out.println("恭喜你中奖" + money);
                break;
            default:
                System.out.println("在买一次把说不定就中了");
                break;

        }
    }

    /**
     * 冒泡排序;蓝球不参与排序;
     * 对数组进行排序;
     */
    public static void Bubbling() {
        for (int i = 0; i < cache.length - 2; i++) {
            for (int j = 0; j < cache.length - 2 - i; j++) {
                if (cache[j] > cache[j + 1]) {
                    int temp = cache[j + 1];
                    cache[j + 1] = cache[j];
                    cache[j] = temp;
                }
            }
        }
    }

    /**
     * 数字格式化类;
     *
     * @param arrs
     */
    public static void lottynumFormat(String[] arrs) {//传进来的数组;
        //数字格式化类;
        DecimalFormat df = new DecimalFormat("00");//格式化的格式;
        for (int i = 0; i < cache.length; i++) { //遍历一边缓存的数组;
            arrs[i] = df.format(cache[i]);//传一个数组过来接收格式化数组的数据;
        }

    }

    /**
     * 输出的方法;
     *
     * @param arrs
     */
    public static void show(String[] arrs) {
        for (int i = 0; i < 6; i++) {//把红的输出;
            System.out.print("红" + (i + 1) + "\t");
        }
        System.out.println("蓝");
        //jdk1.5中foreach相当于遍历,把arrs这个数组进行循环遍历;循环出来的内容起一个别名o;
        for (String o : arrs) {
            System.out.print((String) o + "\t");
        }

    }

    /**
     * 随机生辰数字
     */
    public static void randowNum() {
        for (int i = 0; i < 6; i++) {
            cache[i] = Math.toIntExact(Math.round(Math.random() * 32) + 1);
            if (!RedGlobe(i)) {
                i--;
            }
        }
        for (int i = 6; i < 7; i++) {
            cache[i] = Math.toIntExact(Math.round(Math.random() * 15) + 1);
            if (!blueball(i)) {
                i--;
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值