简单例题:
(Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。
Map)在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯
分析:
1.根据题目可以看出使用Map的键是年份(In'te'r'ge),值是国家(String)
2.在设置输入的时候判断是否是int类型;在设置没有得到输入年份输出没有举办世界杯这里再加一层判断。
实现:
public class newEleven {
public static void main(String[] args) {
//创建Map对象
Map<Integer, String> foot = new TreeMap<Integer, String>();
//初始化数据
foot.put(1930, "乌拉圭");
foot.put(1934, "意大利");
foot.put(1938, "意大利");
foot.put(1950, "乌拉圭");
foot.put(1954, "西德");
foot.put(1958, "巴西");
foot.put(1962, "巴西");
foot.put(1966, "英格兰");
foot.put(1970, "巴西");
foot.put(1974, "西德");
foot.put(1978, "阿根廷");
foot.put(1982, "意大利");
foot.put(1986, "阿根廷");
foot.put(1990, "西德");
foot.put(1994, "巴西");
foot.put(1998, "法国");
foot.put(2002, "巴西");
foot.put(2006, "意大利");
foot.put(2010, "西班牙");
foot.put(2014, "德国");
//
//提出Map中的年份
Set<Integer> key = foot.keySet();
//从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该年没有举办世界杯,则输出:没有举办世界杯。
System.out.println("请输入你想要查询几年的世界杯冠军年份:");
Scanner sc = new Scanner(System.in);
while (true) {
if (sc.hasNextInt()) {
int year = sc.nextInt();
for (Integer y : key) {
if (year == y) {
System.out.println(foot.get(y));
break;
} else if (y == 2014){//当遍历达到最后都没有相同的则执行
System.out.println("没有举办世界杯");
}
}
System.out.println("请输入你要查询的国家:");
Scanner scanner = new Scanner(System.in);
String union = scanner.nextLine();
int max = 0 ;
for (Integer k : key){
if (union.equals(foot.get(k))){
System.out.println(k);
max++;
}
}
if (max == 0){
System.out.println("没有获得过世界杯");
}
return;
} else {
System.out.println("未检测到数据");
return;
}
}
}
}
升华:
模拟斗地主洗牌发牌
分析:
1.首先先创建牌盒,使用HashMap集合。
2.制造牌并把牌加入牌盒。
3.洗牌,可以使用Collections的shuffle()方法。这里就可以使用HashMap中,把键设为索引,值则对应的牌,然后使用集合存储索引。
4.发牌,将每个人以及底牌设计,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌给3个玩家。存放的过程中要求数字大小与斗地主规则的大小对应排序。将代表不同纸牌的数字分配给不同的玩家与底牌。
5. 看牌,通过Map集合找到对应字符展示。通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。
实现:
public class newEight {
public static void main(String[] args) {
//使用Map集合
Map<Integer, String> cardBox = new HashMap<Integer, String>();
//制牌
String[] colors = {"♣", "♥", "♠", "♦"};
String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
int index = 0;//索引
//放入牌盒
for (String number : numbers) {
for (String color : colors) {
String cord = color + number;
cardBox.put(index, cord);
index++;
}
}
cardBox.put(index, "大王");
cardBox.put(index + 1, "小王");
//洗牌
List<Integer> card = new ArrayList<>();//存编号
for (int x = 0; x < cardBox.size(); x++) {
card.add(x);
}
Collections.shuffle(card);
for (Integer s : card) {
System.out.print(cardBox.get(s) + " ");
}
System.out.println();
//发牌
Set<Integer> person1 = new TreeSet<>();
Set<Integer> person2 = new TreeSet<>();
Set<Integer> person3 = new TreeSet<>();
Set<Integer> dp = new TreeSet<>();
for (int x = 0; x < card.size(); x++) {
if (x > card.size() - 3) {
dp.add(card.get(x));
} else if (x % 3 == 0) {
person1.add(card.get(x));
} else if (x % 3 == 1) {
person2.add(card.get(x));
} else {
person3.add(card.get(x));
}
}
//看牌
look("张三", person1, cardBox);
look("李四", person2, cardBox);
look("王五", person3, cardBox);
}
public static void look(String name, Set<Integer> person, Map<Integer, String> cardBox) {
System.out.print(name + "的牌是:");
for (Integer s : person) {
System.out.print(cardBox.get(s) + " ");
}
System.out.println();
}
}