服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java双色球系统开发详解

Java双色球系统开发详解

2022-01-17 11:23烟火9092 Java教程

这篇文章主要为大家详细介绍了Java双色球系统的开发,超级简单的逻辑,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

功能说明

1.1. 菜单页功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package lotteryDemo1;
import java.util.Scanner;
public class Menu {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        o:while(true){
            System.out.println("欢迎使用双色球系统,官方网站www.zhongbuliao.com");
            System.out.println("********请输入菜单选项*********");
            System.out.println("*        1:购买双色球         *");
            System.out.println("*        2:查看开奖           *");
            System.out.println("*        3:查看中奖           *");
            System.out.println("*        4:退出系统           *");
            System.out.println("******************************");
            System.out.println("请输入选项");
            int option =sc.nextInt();
            switch (option){
                case 1:
                    System.out.println("运行购买双色球代码");
                    break;
                case 2:
                    System.out.println("运行查看开奖代码");
                    break;
                case 3:
                    System.out.println("运行查看中奖代码");
                    break;
                case 4:
                    System.out.println("客官常来呀!");
                    break o;
                default:
                    System.out.println("客官不可以");
            }
        }
    }
}

1.2. 购买双色球

1.3. 查看开奖

准备彩票工具类

Java双色球系统开发详解

存放一个生成 彩票的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package lotteryDemo1;
import java.util.Arrays;
public class LotteryUtil {
    public static int[] getLottery(){
        int[] lottery=new int[6];
        for (int i = 0; i <6 ; i++) {
            int num =(int)(Math.random()*33+1);
            while(isContain(lottery,num)){
                num =(int)(Math.random()*33+1);
            }
            lottery[i]=num;
        }
        Arrays.sort(lottery);
        int[] lottery2=new int[7];
        System.arraycopy(lottery,0,lottery2,0,6);
        lottery2[6]=(int)(Math.random()*16+1);
        return lottery2;
    }
    public static boolean isContain(int[] arr,int b){
        boolean flag= false;
        for (int x:arr){
            if(x==b){
                flag=true;
                break;
            }
        }
        return  flag;
    }
}

在 menu类中添加代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package lotteryDemo1;
import java.util.Arrays;
import java.util.Scanner;
public class Menu {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        // 声明一个二维数组 用于存储多个注彩票
        int[][] lotteries=new int[0][];
        int[] luckyNum=new int[0];
        o:while(true){
            System.out.println("欢迎使用双色球系统,官方网站www.zhongbuliao.com");
            System.out.println("********请输入菜单选项*********");
            System.out.println("*        1:购买双色球         *");
            System.out.println("*        2:查看我的双色球     *");
            System.out.println("*        3:查看开奖           *");
            System.out.println("*        4:查看中奖           *");
            System.out.println("*        5:退出系统           *");
            System.out.println("******************************");
            System.out.println("请输入选项");
            int option =sc.nextInt();
            switch (option){
                case 1:
                    System.out.println("运行购买双色球代码");
                    //输入够买的注数
                    System.out.println("请输入购买注数");
                    int zs=sc.nextInt();
                    // 创建一个二维数组 用于存储多个彩票
                    lotteries =new int[zs][];
                    //由程序循环生成彩票
                    for (int i = 0; i <zs ; i++) {
                        lotteries[i] = LotteryUtil.getLottery();
                    }
                    // 提示购买成功
                    System.out.println("购买成功");
                    break;
                case 2:
                    // 购买双色球
                    if(lotteries.length!=0){
                        /*遍历双色球二维数组*/
                        for (int[] lottery:lotteries) {
                            for (int x=0;x<lottery.length;x++) {
                                if(x==5){
                                    System.out.print(lottery[x]+"\t|\t");
                                }else{
                                    System.out.print(lottery[x]+"\t");
                                }
                            }
                            System.out.println();
                        }
                    }else{
                        System.out.println("客官请先花钱购买,然后再查看双色球");
                    }
                    break;
                case 3:
                    // 判断是否购买了双色球
                    // 再生成一注双色球,当成中奖号码即可
                    if(lotteries.length!=0){
                        // 生成中间号码
                        luckyNum=LotteryUtil.getLottery();
                        // 输出中奖号码
                        for (int x=0;x<luckyNum.length;x++) {
                            if(x==5){
                                System.out.print(luckyNum[x]+"\t|\t");
                            }else{
                                System.out.print(luckyNum[x]+"\t");
                            }
                        }
                        System.out.println();
                    }else{
                        System.out.println("客官请先花钱购买,然后再查看开奖号码");
                    }
                    break;
                case 4:
                    System.out.println("查看中奖!");
                    break;
                case 5:
                    System.out.println("客官再来呀!");
                    break o;
                default:
                    System.out.println("客官不可以");
            }
        }
    }
}

1.4. 查看中奖

在LotteryUtil中定义一个判断中奖的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static int isWin(int[] luckyNum,int[] lottery){
    // 判断前六位有几位相同
    int red=0;
    for (int i = 0; i <6 ; i++) {
        System.out.print(lottery[i]+"\t");
        if(luckyNum[i]==lottery[i]){
            red++;
        }
    }
    int blue=luckyNum[6]==lottery[6]?1:0;
    System.out.print("\t|\t"+lottery[6]+"\t");
    int money=0;
    if(red==6&&blue==1){
        money=5000000;
        System.out.println("一等奖5000000元");
    }else if(red==6){
        money=1250000;
        System.out.println("二等奖1250000元");
    }else if(red==5&&blue==1){
        money=3000;
        System.out.println("三等奖3000元");
    }else if(red==5||red+blue==5){
        money=200;
        System.out.println("四等奖200元");
    }else if(red==4||red+blue==4){
        money=10;
        System.out.println("五等奖10元");
    } else if(red==0&&blue==1){
        money=5;
        System.out.println("六等奖5元");
    }else{
        System.out.println("未中奖0元");
    }
    return money;
}

在menu中添加查看中奖功能

Java双色球系统开发详解

1.5. 退出系统

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_45859844/article/details/120414080

延伸 · 阅读

精彩推荐