一, 双色球小游戏
双色球类 (6 红 1 蓝):
- package top.liaoyingpeng.bean;
- import java.util.Arrays;
- public class Balls {
- private int[] red = new int[6];
- private int blue;
- private BallBox makeBy;
- // 全机器生成
- protected Balls(BallBox bx) {
- makeBy = bx;
- }
- // 比较
- public byte[] equals(Balls obj) {
- byte[] num = { 0, 0 };
- if (blue == obj.blue) {
- num[1] = 1;
- } else {
- num[1] = 0;
- }
- for (int i : obj.red) {
- for (int j : this.red) {
- if (i == j)
- num[0]++;
- }
- }
- return num;
- }
- // 设置球
- public int getRed(int n) {
- return this.red[n];
- }
- public int[] getAllRed(){
- return red;
- }
- protected void setRed(int n, int red) {
- this.red[n] = red;
- }
- public int getBlue() {
- return blue;
- }
- protected void setBlue(int blue) {
- this.blue = blue;
- makeBy.blue = true;
- }
- // 显示全部
- @Override
- public String toString() {
- return Arrays.toString(red) + "[" + blue + "]";
- }
- }
球箱类 (生成双色球):
- package top.liaoyingpeng.bean;
- import java.util.LinkedHashSet;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class BallBox {
- private LinkedHashSet<Integer> red = new LinkedHashSet<Integer>();// 1-33 已抽出的红球
- Scanner sc = new Scanner(System.in);
- private Balls Making = null;// 正在抽的球组
- private Random rd = new Random();
- protected boolean blue = false;// 标识蓝色球是否抽出
- // 球填充
- public BallBox() {
- }
- // 获得随机双色球组
- public Balls getRandomBalls() {
- if (!isUsed()) {
- Making = new Balls(this);
- int num;
- for (int i = 0 + red.size(); i < 6; i++) {
- do {
- num = rd.nextInt(33) + 1;// 1-33
- } while (red.contains(num));
- red.add(num);
- Making.setRed(i, num);
- }
- if (!blue) {
- Making.setBlue(rd.nextInt(16) + 1);
- }// 1-16
- return Making;
- } else {
- return null;
- }
- }
- // 购买双色球
- public Balls buy() {
- if (!isUsed()) {
- System.out.print("请输入要购买的球号 \ n 格式: [01, 23, 12, 11, 24, 05][15]\n 您要购买:");
- int ball[] = null;
- while (ball == null) {
- ball = checkNum(sc.nextLine());
- }
- /* if (ball != null) {*/
- Making = new Balls(this);
- for (int i = 0; i < 6; i++) {
- Making.setRed(i, ball[i]);
- }
- Making.setBlue(ball[6]);
- return Making;
- /* } else {
- return null;
- }*/
- } else {
- return null;
- }
- }
- // 格式检测
- private int[] checkNum(String balls) {
- int[] ball = new int[7];
- Pattern p = Pattern.compile("[0-9][0-9]");
- Matcher m = p.matcher(balls);
- int i;
- for (i = 0; i < 10; i++) {// 容错
- if (m.find()) {
- ball[i] = new Integer(m.group());
- if (ball[i] > 33 || ball[i] == 0) {
- break;
- }
- } else {
- break;
- }
- }
- // System.out.println(i);
- if (ball[6] <= 16 && i == 7// 应正好 7 个
- && 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*")) {
- return ball;
- } else {
- System.out.println("格式或球号错误 请重新输入");
- return null;
- }
- }
- // 重置球箱
- public void clear() {
- red.clear();
- blue = false;
- Making = null;
- }
- // 是否已抽出蓝色球
- public boolean isBlued() {
- return blue;
- }
- // 已抽出红球个数
- public int alRed() {
- return red.size();
- }
- // 是否以抽完
- public boolean isUsed() {
- if (red.size() == 6 && blue)
- return true;
- else
- return false;
- }
- // 是否正在或已使用
- public boolean isUsing() {
- if (Making != null)
- return true;
- else
- return false;
- }
- }
主界面 (main):
- package top.liaoyingpeng.view;
- import java.util.Scanner;
- import top.liaoyingpeng.bean.BallBox;
- import top.liaoyingpeng.bean.Balls;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- static int[] hasPrize = { 0, 0, 0, 0, 0, 0, 0 };// 1-6 等奖 奖金;
- static int times = 0;// 抽奖次数
- public static void main(String[] args) {
- System.out.println("********** 双色球游戏 **********");
- System.out
- .println("规则:\n\t 选择 6 个红球和 1 个蓝球 \n\t 红色球号码从 1--33 中选择 \ n\t 蓝色球号码从 1--16 中选择");
- System.out.println("\t 由系统随机生成一组双色球 \ n\t 用户进行单式投注 \ n\n 按回车键开始投注");
- sc.nextLine();
- while (game())
- ;
- System.out.println("游戏结束 --- 退出 ing");
- sc.close();
- }
- public static boolean game() {
- System.out.println("******************************");
- BallBox bx = new BallBox();
- Balls player;
- Balls gover;
- System.out.println("请选择:\n\t1) 手动下注 \ n\t2) 自动下注");
- if ("1".equals(sc.nextLine())) {
- player = bx.buy();
- } else {
- player = bx.getRandomBalls();
- System.out.println("您购买了:" + player.toString());
- }
- bx.clear();// 重置球箱
- gover = bx.getRandomBalls();
- byte[] result = player.equals(gover);
- int prize = prize(result);
- System.out.println("开奖情况:" + gover.toString());
- System.out.println("您共猜中红球" + result[0] + "个, 蓝球" + result[1] + "个 \ n");
- if (prize > 0) {
- String lv = level(prize);
- System.out.println("恭喜您获得" + lv + "奖金" + prize + "元");
- } else {
- System.out.println("抱歉 您未获奖 \ n******************************");
- }
- times++;
- showPrize();
- System.out.println("按回车再来一次 \ n 输入 exit 退出游戏");
- if ("exit".equals(sc.nextLine()))
- return false;
- else
- return true;
- }
- public static void showPrize() {
- System.out.println("您已获得:\n\t 一等奖" + hasPrize[0] + "次");
- System.out.println("\t 二等奖" + hasPrize[1] + "次");
- System.out.println("\t 三等奖" + hasPrize[2] + "次");
- System.out.println("\t 四等奖" + hasPrize[3] + "次");
- System.out.println("\t 五等奖" + hasPrize[4] + "次");
- System.out.println("\t 六等奖" + hasPrize[5] + "次");
- System.out.println("\t\t 獎金共" + hasPrize[6] + "元");
- System.out.println("\t\t 投注共" + times + "注 \ n");
- }
- public static int prize(byte[] result) {
- int prize = 0;
- switch (result[0]) {
- case 6: {
- if (result[1] == 1)
- prize = 10000000;
- else
- prize = 3000000;
- break;
- }
- case 5: {
- if (result[1] == 1)
- prize = 3000;
- else
- prize = 200;
- break;
- }
- case 4: {
- if (result[1] == 1)
- prize = 200;
- else
- prize = 10;
- break;
- }
- case 3: {
- if (result[1] == 1)
- prize = 10;
- break;
- }
- case 2: {
- if (result[1] == 1)
- prize = 5;
- break;
- }
- case 1: {
- if (result[1] == 1)
- prize = 5;
- break;
- }
- case 0: {
- if (result[1] == 1)
- prize = 5;
- break;
- }
- }
- hasPrize[6] += prize;
- return prize;
- }
- public static String level(int prize) {
- switch (prize) {
- case 10000000:
- hasPrize[0]++;
- return "一等奖";
- case 3000000:
- hasPrize[1]++;
- return "二等奖";
- case 3000:
- hasPrize[2]++;
- return "三等奖";
- case 200:
- hasPrize[3]++;
- return "四等奖";
- case 10:
- hasPrize[4]++;
- return "五等奖";
- case 5:
- hasPrize[5]++;
- return "六等奖";
- default:
- return "未中奖";
- }
- }
- }
- ----------------------------------------------------------------------------------------------------------------------------------
这个双色球小游戏是我在体验营的一个小作业, 本意是练习数组的使用, 但是因为我有在自学一点相关知识, 就尝试使用了正则表达式以及面向对象的写法, 在写这个的时候我才刚刚接触 Java 不久, 类之间的依赖关系感觉还不太好, 索性还有点 c 语言的基础, 学起 Java 感觉也很有趣, 但面向对象的思想还需要多多练习.
----------------------------------------------------------------------------------------------------------------------------------
二, 自我介绍
本人目前是一名大一在校生, 原本是机械专业, 在上学期加入了一个科创类社团, 第一次真正接触程序设计. 接触之后我就深深地被其所吸引, 并在下学期下定决心转专业到了计算机专业.
在社团我是做单片机编程的, 个人对网站开发也有些兴趣, 日后也希望在计算机行业就业, 所以报名了一个暑期体验营学习 Java, 体验营的老师建议我们早些开始写自己的技术博客, 固在斟酌之后决定在这里开始自己的第一篇博客. 就算没有多少人看, 我也想坚持下去, 不济也可以当作自己的小笔记, 若是以后自己的文章能够帮助某个人解决某个问题, 那就很开心了, 毕竟之前遇到的一些问题都是在大家的博客上得到的解决.
希望在这里和广大社区的朋友共同进步, 感受程序的魅力.
来源: http://www.bubuko.com/infodetail-2686108.html