当前位置:网站首页>集合练习程序
集合练习程序
2022-07-21 18:39:00 【华为云】
1:输入字符串统计其中各个字符出现的次数
package java_practice;import java.util.HashMap;import java.util.Scanner;import java.util.Set;//实现一个统计输入字符串中出现某字符的个数public class HashMapDemo2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串"); String line = sc.nextLine(); //创建HashMap集合 HashMap<Character, Integer> hm = new HashMap<>(); for (int i = 0; i < line.length(); i++) { char key = line.charAt(i); Integer value = hm.get(key); if (value == null) { hm.put(key, 1); } else { value++; hm.put(key, value); } } StringBuilder sb = new StringBuilder(); Set<Character> keyset = hm.keySet(); for(Character key:keyset) { Integer value = hm.get(key); sb.append(key).append("(").append(value).append(")"); } String result = sb.toString(); System.out.println(result); }}
2:模拟斗地主发牌洗牌
–jgdabc(兰舟千帆)
版本一
package demo;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;public class PokerDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); //创建HashMap,键是编号,值是牌 HashMap<Integer, String> hm = new HashMap<>(); //创建ArrayList,存储编号 //定义花色数组 String[] colors = {"", "", "", ""}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; // for (String color : colors) for (String number : numbers) { array.add(color + number); } array.add("小王"); array.add("大王"); //System.out.println(array); //进行洗牌 Collections.shuffle(array); // System.out.println(array); //创建三个玩家 ArrayList<String> lqx = new ArrayList<String>(); ArrayList<String> ly = new ArrayList<>(); ArrayList<String> fqy = new ArrayList<>(); //创建底牌 ArrayList<String> dp = new ArrayList<>(); //进行发牌 for (int i = 0; i < array.size(); i++) { String s = array.get(i); if (i >= array.size() - 3) { dp.add(s); } else if (i % 3 == 0) { lqx.add(s); } else if (i % 3 == 1) { ly.add(s); } else if (i % 3 == 2) { fqy.add(s); } } lookPoker("林青霞",lqx); lookPoker("柳岩",ly); lookPoker("风清扬",fqy); lookPoker("底牌",dp); } //看牌的方法 public static void lookPoker(String name,ArrayList<String> array) { System.out.print(name + "的牌是:"); for(String poker:array) { System.out.print(poker+" "); } System.out.println(); } //存储编号 // }
版本二
package demo;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.TreeSet;public class PokerDemo1 { public static void main(String args[]) { //创建HashMap,键是编号,值是牌 HashMap<Integer, String> hm = new HashMap<Integer, String>(); //创建ArrayList。存储编号 ArrayList<Integer> array = new ArrayList<>(); //创建花色数组和点数数组 String[] colors = {"", "", "", ""}; String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //从零开始往HashMap中存储编号,并存储对应的牌,同时在ArrayList里面存储编号 int index = 0; for (String number : numbers) { for (String color : colors) { hm.put(index, color + number); array.add(index); index++; } } hm.put(index, "小王"); array.add(index); index++; hm.put(index, "大王"); array.add(index); Collections.shuffle(array); //发牌,发的也是编号,为了保证编号是排序的,创建TreeSet集合接收 TreeSet<Integer> lqx = new TreeSet<Integer>(); TreeSet<Integer> ly = new TreeSet<Integer>(); TreeSet<Integer> fqy = new TreeSet<Integer>(); TreeSet<Integer> dp = new TreeSet<Integer>(); for (int i = 0; i < array.size(); i++) { int x = array.get(i); if (i >= array.size() - 3) { dp.add(x); } else if (i % 3 == 0) { lqx.add(x); } else if (i % 3 == 1) { ly.add(x); } else if (i % 3 == 2) { fqy.add(x); } } lookPoker("林青霞", lqx, hm); lookPoker("柳岩", ly, hm); lookPoker("风清扬",fqy,hm); lookPoker("底牌",dp,hm); } //定义方法看牌(遍历TreeSet集合,获取编号,到HashMap找到对应的牌) public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) { System.out.println(name + "的牌是:"); for (Integer key : ts) { String poker = hm.get(key); System.out.print(poker + " "); } System.out.println(); }}
边栏推荐
- Deploy the jar package of Ruiji takeout project on the remote server and successfully run on the PC and mobile terminal
- 互联网寒冬,3个月如何从功能测试进阶自动化测试?【附学习指南】
- Win10如何把图标发送到桌面
- About for In and for Of understanding and use
- 狂神redis笔记07
- 【OpenCV 例程300篇】234. 特征提取之主成分分析(PCA)
- Live broadcast preview │ special session of Zhihui Yunzhou "digital twin smart Park solutions"
- 时间复杂度吐血总结
- 《机器人SLAM导航核心技术与实战》第1季:第1章_ROS入门必备知识
- Section 18 of Chapter 2: character set and coding
猜你喜欢
Introduction to MVC framework
关于IE6浏览器出现的问题的解决
Servlet的生命周期
#夏日挑战赛#【FFH】NFC碰一碰拉起任何应用,无需企业认证!
網絡爬蟲爬取b站勵志彈幕並生成詞雲(精心筆記總結)
直播预告│智汇云舟“数字孪生智慧园区解决方案”专场
opencv实现银行卡号识别
Robot slam navigation core technology and practice Season 1: Chapter 1_ Necessary knowledge for ROS entry
Les Crawlers du réseau rampent sur le rideau d'excitation de la station B et génèrent des nuages de mots (résumé détaillé des notes)
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
随机推荐
Andorid view activity task stack
Mxcad5.2 20190704 update
N分钟学会分位值的计算方式
Section 4 of Chapter 3: formal parameters
Live broadcast preview │ special session of Zhihui Yunzhou "digital twin smart Park solutions"
day02
Intelligent science innovation lecture hall | Institute of automation he Huiguang: Research on deep learning brain mechanism based on visual information encoding and decoding
大量连接时使用 使用epoll管理 or golang 多协程
Is it safe to open an account on flush? ETF trading rules and fees
EMQX v4.4.5 发布:新增排他订阅及 MQTT 5.0 发布属性支持
EasyCVR平台级联时,出现报错提示端口不可达是什么原因?
How 3D video fusion technology enables smart city construction
黑马瑞吉外卖之后台登录与退出功能开发
Time complexity hematemesis summary
CocosCreator手游多屏幕适配
Biochemistry review II. Nucleic acid
Wechat applet navigation bar sliding transparency change
How to efficiently backup local data to Tencent cloud
DHCP 以及 DHCP 的工作原理
Deploy the jar package of Ruiji takeout project on the remote server and successfully run on the PC and mobile terminal