Java之集合常见例题(1、统计字符出现次数2、集合嵌套遍历。3、斗地主)
一、统计字符串中字符出现的次数(数字、字母、汉字都可以)
package com.westos.GuessNumber;
import java.util.Scanner;
/** * 猜数字:随机生成1-100之间的数字。由用户输入值进行比对,返回猜测的次数 * 不足:无法完成连续多次持续玩游戏。 * @author 大虾 * */
public class GuessNumber {
public static int getRandom (int start,int end) {
return (int) (Math.random()*(end-start)+start);
}
public void guessNumber() {
System.out.println("============猜数字游戏开始=========\n");
System.out.println("请输入1-100之间的数字");
int random = getRandom(1, 100);
int count = 0;
while(true) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
Integer num = Integer.parseInt(str);
if(num == 0) {
System.out.println("再玩一次!");
guessNumber();
break;
}
if(num == -1) {
System.out.println("游戏结束,再见!");
System.exit(0);
}
if(num > random) {
System.out.println("您输入的值" + num + "大了" );
count++;
}
if(num < random) {
System.out.println("您输入的值" + num + "小了");
count++;
}
if(num == random) {
count++;
System.out.println("Congratulations!经过"+ count + "次, 您猜对了");
System.out.println("======Continue? 0继续/-1退出=========\n");
continue;
}
}
}
public static void main(String[] args) {
GuessNumber gn = new GuessNumber();
gn.guessNumber();
}
}
二、集合的嵌套遍历(万剑不离其宗,只举一个例子)
package org.wests.HomeWork03;
import java.util.HashMap;
/** * 集合的嵌套 * A:案例演示 集合嵌套之HashMap嵌套HashMap 网星软件 jc 基础班 张三 20 李四 22 jy 就业班 王五 21 赵六 23 * @author 代虎 * */
public class HomeWork01 {
public static void main(String[] args) {
HashMap<String,HashMap<String,Integer>> map = new HashMap<String, HashMap<String,Integer>>();
HashMap<String, Integer> hashMap01 = new HashMap<String,Integer>();
hashMap01.put("张三", 20);
hashMap01.put("李四", 22);
HashMap<String, Integer> hashMap02 = new HashMap<String,Integer>();
hashMap02.put("王五", 21);
hashMap02.put("赵六", 23);
map.put("基础班", hashMap01);
map.put("就业班", hashMap02);
for(String str:map.keySet()) {
System.out.println(str);
for(String str01:map.get(str).keySet()) {
System.out.println("\t" + str01 + map.get(str).get(str01));
}
}
}
}
三、编写程序完成斗地主模式
package com.westos.chinesePoker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/** *加强版的斗地主 EnhancedVersion *在version1.0的基础上加入扑克排序. * @author 大虾 * @version 2.0 */
public class ChinesePokerEnhanced {
public static void main(String[] args) {
//0-53代表每个对应的扑克
Map<Integer, String> map = new HashMap<Integer,String>();
List<String> list = new ArrayList<>();
String[] color = {
"♥","♠","♣","◆"};
String[] num = {
"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for(String str:num) {
for(String str1:color) {
String poker = str1.concat(str);
list.add(poker);
}
}
list.add("♀");
list.add("♂");
for(int i=0;i<54;i++) {
map.put(i, list.get(i));
}
Set<Integer> keyset = map.keySet();
List<Integer> list1 = new ArrayList<>();
for(Integer count:keyset){
list1.add(count);
}
//洗牌三次
Collections.shuffle(list1);
Collections.shuffle(list1);
Collections.shuffle(list1);
Set<Integer> player01 = new TreeSet<>();
Set<Integer> player02 = new TreeSet<>();
Set<Integer> player03 = new TreeSet<>();
Set<Integer> dipai = new TreeSet<>();
//发牌
for(int i=0;i<list1.size();i++) {
if(i<51&&i%3 == 0) {
player01.add(list1.get(i));
}
if(i<51&&i%3 == 1) {
player02.add(list1.get(i));
}
if(i<51&&i%3 == 2) {
player03.add(list1.get(i));
}if(i>=51)
dipai.add(list1.get(i));
}
//玩家一的牌
System.out.println("玩家一:");
for(Integer i:player01) {
System.out.print(map.get(i)+" ");
}
System.out.println("\n玩家二:");
//玩家二的牌
for(Integer i:player02) {
System.out.print(map.get(i)+ " ");
}
System.out.println("\n玩家三:");
//玩家三的牌
for(Integer i:player03) {
System.out.print(map.get(i)+ " ");
}
System.out.println("\n底牌:");
//玩家一的牌
for(Integer i:dipai) {
System.out.print(map.get(i)+ " ");
}
}
}