zoukankan      html  css  js  c++  java
  • 第一篇博客:一个双色球游戏 、以及个人介绍

    一、双色球小游戏

    双色球类(6红1蓝):

     1 package top.liaoyingpeng.bean;
     2 
     3 import java.util.Arrays;
     4 
     5 public class Balls {
     6     private int[] red = new int[6];
     7     private int blue;
     8     private BallBox makeBy;
     9 
    10     // 全机器生成
    11     protected Balls(BallBox bx) {
    12         makeBy = bx;
    13     }
    14 
    15     // 比较
    16     public byte[] equals(Balls obj) {
    17         byte[] num = { 0, 0 };
    18         if (blue == obj.blue) {
    19             num[1] = 1;
    20         } else {
    21             num[1] = 0;
    22         }
    23 
    24         for (int i : obj.red) {
    25             for (int j : this.red) {
    26                 if (i == j)
    27                     num[0]++;
    28             }
    29         }
    30         return num;
    31     }
    32 
    33     // 设置球
    34     public int getRed(int n) {
    35         return this.red[n];
    36     }
    37 
    38     public int[] getAllRed(){
    39         return red;
    40     }
    41     
    42     protected void setRed(int n, int red) {
    43         this.red[n] = red;
    44     }
    45 
    46     public int getBlue() {
    47         return blue;
    48     }
    49 
    50     protected void setBlue(int blue) {
    51         this.blue = blue;
    52         makeBy.blue = true;
    53     }
    54 
    55     // 显示全部
    56     @Override
    57     public String toString() {
    58 
    59         return Arrays.toString(red) + "[" + blue + "]";
    60 
    61     }
    62 }
    双色球组

    球箱类(生成双色球):

      1 package top.liaoyingpeng.bean;
      2 
      3 import java.util.LinkedHashSet;
      4 import java.util.Random;
      5 import java.util.Scanner;
      6 import java.util.regex.Matcher;
      7 import java.util.regex.Pattern;
      8 
      9 public class BallBox {
     10     private LinkedHashSet<Integer> red = new LinkedHashSet<Integer>();// 1-33已抽出的红球
     11     Scanner sc = new Scanner(System.in);
     12     private Balls Making = null;// 正在抽的球组
     13     private Random rd = new Random();
     14     protected boolean blue = false;// 标识蓝色球是否抽出
     15 
     16     // 球填充
     17     public BallBox() {
     18     }
     19 
     20     // 获得随机双色球组
     21     public Balls getRandomBalls() {
     22         if (!isUsed()) {
     23             Making = new Balls(this);
     24             int num;
     25             for (int i = 0 + red.size(); i < 6; i++) {
     26                 do {
     27                     num = rd.nextInt(33) + 1;// 1-33
     28                 } while (red.contains(num));
     29                 red.add(num);
     30                 Making.setRed(i, num);
     31             }
     32             if (!blue) {
     33                 Making.setBlue(rd.nextInt(16) + 1);
     34             }// 1-16
     35             return Making;
     36         } else {
     37             return null;
     38         }
     39     }
     40 
     41     // 购买双色球
     42     public Balls buy() {
     43         if (!isUsed()) {
     44             System.out.print("请输入要购买的球号
    格式:  [01, 23, 12, 11, 24, 05][15]
    您要购买:");
     45             int ball[] = null;
     46             while (ball == null) {
     47                 ball = checkNum(sc.nextLine());
     48             }
     49 /*            if (ball != null) {*/
     50                 Making = new Balls(this);
     51                 for (int i = 0; i < 6; i++) {
     52                     Making.setRed(i, ball[i]);
     53                 }
     54                 Making.setBlue(ball[6]);
     55                 return Making;
     56 /*            } else {
     57                 return null;
     58             }*/
     59         } else {
     60             return null;
     61         }
     62     }
     63 
     64     // 格式检测
     65     private int[] checkNum(String balls) {
     66         int[] ball = new int[7];
     67         Pattern p = Pattern.compile("[0-9][0-9]");
     68         Matcher m = p.matcher(balls);
     69         int i;
     70         for (i = 0; i < 10; i++) {//容错
     71             if (m.find()) {
     72                 ball[i] = new Integer(m.group());
     73                 if (ball[i] > 33 || ball[i] == 0) {
     74                     break;
     75                 }
     76             } else {
     77                 break;
     78             }
     79         }
     80         // System.out.println(i);
     81         if (ball[6] <= 16 && i == 7//应正好7个
     82                 && balls.matches("\s*(\[\s*\d{2}\s*,\s*\d{2}\s*,\s*\d{2}\s*,\s*\d{2}\s*,\s*\d{2}\s*,\s*\d{2}\s*\])(\[\d\d\])\s*")) {
     83             return ball;
     84         } else {
     85             System.out.println("格式或球号错误 请重新输入");
     86             return null;
     87         }
     88     }
     89 
     90     // 重置球箱
     91     public void clear() {
     92         red.clear();
     93         blue = false;
     94         Making = null;
     95     }
     96 
     97     // 是否已抽出蓝色球
     98     public boolean isBlued() {
     99         return blue;
    100     }
    101 
    102     // 已抽出红球个数
    103     public int alRed() {
    104         return red.size();
    105     }
    106 
    107     // 是否以抽完
    108     public boolean isUsed() {
    109         if (red.size() == 6 && blue)
    110             return true;
    111         else
    112             return false;
    113     }
    114 
    115     // 是否正在或已使用
    116     public boolean isUsing() {
    117         if (Making != null)
    118             return true;
    119         else
    120             return false;
    121     }
    122 }
    球箱类

    主界面(main):

      1 package top.liaoyingpeng.view;
      2 
      3 import java.util.Scanner;
      4 
      5 import top.liaoyingpeng.bean.BallBox;
      6 import top.liaoyingpeng.bean.Balls;
      7 
      8 public class Test {
      9     static Scanner sc = new Scanner(System.in);
     10     static int[] hasPrize = { 0, 0, 0, 0, 0, 0, 0 };// 1-6等奖 奖金;
     11     static int times = 0;//抽奖次数
     12 
     13     public static void main(String[] args) {
     14 
     15         System.out.println("**********双色球游戏**********");
     16         System.out
     17                 .println("规则:
    	选择6个红球和1个蓝球 
    	红色球号码从1--33中选择
    	蓝色球号码从1--16中选择");
     18         System.out.println("	由系统随机生成一组双色球
    	用户进行单式投注
    
    按回车键开始投注");
     19         sc.nextLine();
     20         while (game())
     21             ;
     22         System.out.println("游戏结束---退出ing");
     23         sc.close();
     24     }
     25 
     26     public static boolean game() {
     27         System.out.println("******************************");
     28         BallBox bx = new BallBox();
     29         Balls player;
     30         Balls gover;
     31         System.out.println("请选择:
    	1)手动下注
    	2)自动下注");
     32         if ("1".equals(sc.nextLine())) {
     33             player = bx.buy();
     34         } else {
     35             player = bx.getRandomBalls();
     36             System.out.println("您购买了:" + player.toString());
     37         }
     38         bx.clear();// 重置球箱
     39         gover = bx.getRandomBalls();
     40         byte[] result = player.equals(gover);
     41         int prize = prize(result);
     42 
     43         System.out.println("开奖情况:" + gover.toString());
     44         System.out.println("您共猜中红球" + result[0] + "个,蓝球" + result[1] + "个
    ");
     45         if (prize > 0) {
     46             String lv = level(prize);
     47             System.out.println("恭喜您获得" + lv + " 奖金" + prize + "元");
     48         } else {
     49             System.out.println("抱歉 您未获奖
    ******************************");
     50         }
     51         times++;
     52         showPrize();
     53         System.out.println("按回车再来一次
    输入exit退出游戏");
     54         if ("exit".equals(sc.nextLine()))
     55             return false;
     56         else
     57             return true;
     58     }
     59 
     60     public static void showPrize() {
     61         System.out.println("您已获得:
    	一等奖 " + hasPrize[0] + " 次");
     62         System.out.println("	二等奖 " + hasPrize[1] + " 次");
     63         System.out.println("	三等奖 " + hasPrize[2] + " 次");
     64         System.out.println("	四等奖 " + hasPrize[3] + " 次");
     65         System.out.println("	五等奖 " + hasPrize[4] + " 次");
     66         System.out.println("	六等奖 " + hasPrize[5] + " 次");
     67         System.out.println("		獎金共 " + hasPrize[6] + " 元");
     68         System.out.println("		投注共 " + times + " 注
    ");
     69     }
     70 
     71     public static int prize(byte[] result) {
     72         int prize = 0;
     73         switch (result[0]) {
     74         case 6: {
     75             if (result[1] == 1)
     76                 prize = 10000000;
     77             else
     78                 prize = 3000000;
     79             break;
     80         }
     81         case 5: {
     82             if (result[1] == 1)
     83                 prize = 3000;
     84             else
     85                 prize = 200;
     86             break;
     87         }
     88         case 4: {
     89             if (result[1] == 1)
     90                 prize = 200;
     91             else
     92                 prize = 10;
     93             break;
     94         }
     95         case 3: {
     96             if (result[1] == 1)
     97                 prize = 10;
     98             break;
     99         }
    100         case 2: {
    101             if (result[1] == 1)
    102                 prize = 5;
    103             break;
    104         }
    105         case 1: {
    106             if (result[1] == 1)
    107                 prize = 5;
    108             break;
    109         }
    110         case 0: {
    111             if (result[1] == 1)
    112                 prize = 5;
    113             break;
    114         }
    115         }
    116         hasPrize[6] += prize;
    117         return prize;
    118     }
    119 
    120     public static String level(int prize) {
    121         switch (prize) {
    122         case 10000000:
    123             hasPrize[0]++;
    124             return "一等奖";
    125         case 3000000:
    126             hasPrize[1]++;
    127             return "二等奖";
    128         case 3000:
    129             hasPrize[2]++;
    130             return "三等奖";
    131         case 200:
    132             hasPrize[3]++;
    133             return "四等奖";
    134         case 10:
    135             hasPrize[4]++;
    136             return "五等奖";
    137         case 5:
    138             hasPrize[5]++;
    139             return "六等奖";
    140         default:
    141             return "未中奖";
    142         }
    143     }
    144 }
    主界面

    ----------------------------------------------------------------------------------------------------------------------------------

    这个双色球小游戏是我在体验营的一个小作业,本意是练习数组的使用,但是因为我有在自学一点相关知识,就尝试使用了正则表达式以及面向对象的写法,在写这个的时候我才刚刚接触Java不久,类之间的依赖关系感觉还不太好,索性还有点c语言的基础,学起Java感觉也很有趣,但面向对象的思想还需要多多练习。

    ----------------------------------------------------------------------------------------------------------------------------------

    二、自我介绍

            本人目前是一名大一在校生,原本是机械专业,在上学期加入了一个科创类社团,第一次真正接触程序设计。接触之后我就深深地被其所吸引,并在下学期下定决心转专业到了计算机专业。

            在社团我是做单片机编程的,个人对网站开发也有些兴趣,日后也希望在计算机行业就业,所以报名了一个暑期体验营学习Java,体验营的老师建议我们早些开始写自己的技术博客,固在斟酌之后决定在这里开始自己的第一篇博客。就算没有多少人看,我也想坚持下去,不济也可以当作自己的小笔记,若是以后自己的文章能够帮助某个人解决某个问题,那就很开心了,毕竟之前遇到的一些问题都是在大家的博客上得到的解决。

        希望在这里和广大社区的朋友共同进步,感受程序的魅力。

  • 相关阅读:
    数组和对象常用方法汇总
    基于vue的悬浮碰撞窗口(用于打广告的)组件
    时间的基本处理
    防抖动和节流阀
    A. 配置xftp和xshell来远程管理Linux服务器
    课堂练习-找水王
    评价软件
    构建之法阅读笔记02
    学习进度条博客11
    用户场景
  • 原文地址:https://www.cnblogs.com/ACTIM/p/ACTIM_firstPassage.html
Copyright © 2011-2022 走看看