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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 咖啡苏克 中级黑马   /  2014-7-11 22:27  /  2017 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1.          import java.util.ArrayList;
  2.          import java.util.Collections;
  3.          import java.util.HashMap;
  4.          import java.util.TreeSet;

  5. /*
  6. * HashMap:牌的编号,及牌
  7. * ArrayList:牌的编号
  8. * TreeSet:牌的编号        --        输出的数据是根据编号从Map集合中找到的对应的值
  9. */
  10. public class PokerDemo {
  11.         public static void main(String[] args) {
  12.                 // 创建HashMap集合
  13.                 HashMap<Integer, String> hm = new HashMap<Integer, String>();
  14.                 // 创建ArrayList集合
  15.                 ArrayList<Integer> array = new ArrayList<Integer>();

  16.                 // 定义一个花色数组
  17.                 String[] colors = { "♦", "♣", "♥", "♠" };
  18.                 // 定义纸牌的数值
  19.                 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
  20.                                 "K", "A", "2" };

  21.                 // 把牌定义完成
  22.                 int index = 0;
  23.                 for (String number : numbers) {
  24.                         for (String color : colors) {
  25.                                 hm.put(index, color.concat(number)); // {0,♦3},{1,♣3}
  26.                                 array.add(index); // 0,1,
  27.                                 index++; // index=1,index=2
  28.                         }
  29.                 }
  30.                 hm.put(index, "小王");
  31.                 array.add(index);
  32.                 index++;
  33.                 hm.put(index, "大王");
  34.                 array.add(index);

  35.                 // 洗牌
  36.                 Collections.shuffle(array);

  37.                 // 发牌
  38.                 TreeSet<Integer> liuBei = new TreeSet<Integer>();
  39.                 TreeSet<Integer> caoCao = new TreeSet<Integer>();
  40.                 TreeSet<Integer> sunQuan = new TreeSet<Integer>();
  41.                 ArrayList<Integer> diPai = new ArrayList<Integer>();

  42.                 for (int x = 0; x < array.size(); x++) {
  43.                         if (x >= array.size() - 3) {
  44.                                 diPai.add(array.get(x));
  45.                         } else if (x % 3 == 0) {
  46.                                 liuBei.add(array.get(x));
  47.                         } else if (x % 3 == 1) {
  48.                                 caoCao.add(array.get(x));
  49.                         } else if (x % 3 == 2) {
  50.                                 sunQuan.add(array.get(x));
  51.                         }
  52.                 }

  53.                 // 看牌
  54.                 System.out.print("刘备的牌:");
  55.                 for (Integer i : liuBei) {
  56.                         String card = hm.get(i);
  57.                         System.out.print(card + " ");
  58.                 }
  59.                 System.out.println();
  60.                 System.out.print("曹操的牌:");
  61.                 for (Integer i : caoCao) {
  62.                         String card = hm.get(i);
  63.                         System.out.print(card + " ");
  64.                 }
  65.                 System.out.println();
  66.                 System.out.print("孙权的牌:");
  67.                 for (Integer i : sunQuan) {
  68.                         String card = hm.get(i);
  69.                         System.out.print(card + " ");
  70.                 }
  71.                 System.out.println();
  72.                 System.out.print("底牌:");
  73.                 for (Integer i : diPai) {
  74.                         String card = hm.get(i);
  75.                         System.out.print(card + " ");
  76.                 }
  77.                 System.out.println();
  78.         }
  79. }
复制代码

涉及到了ArrayList、TreeSet还有HashMap集合,挺有意思,可以看一下。。。

4 个回复

倒序浏览
代码看起来不多,问题是你能保证它运行时不出错,还有代码的正确性吗?
回复 使用道具 举报
自闭宅男 发表于 2014-7-12 01:13
代码看起来不多,问题是你能保证它运行时不出错,还有代码的正确性吗?

运行没问题啊!验证过了...
回复 使用道具 举报
很好   花色能改一下就好了
回复 使用道具 举报
用DOS运行出来的都是问号加一个数字  不清楚!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马