package com.heima.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Demo5_Scanner {
/**
* @param args
* 周末又到了,今天是一道有趣的题目! 请往下看
题目:
1.要求定义A-K 13张牌 分别代表 1-13点分数
2.本人和电脑 每人随机分三张牌显示在控制台 要求纯随机
3.然后比较你和电脑三张牌的点数之和的大小,分出谁是赢家,并赢了多少点,输了多少点或打平
*/
public static void main(String[] args) {
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] color = {"黑","红","梅","方"};
String[][] poker = new String[13][4];
for (int i = 0; i < poker.length; i++) {
for (int j = 0; j < poker[i].length; j++) {
poker[i][j] = color[j] + num[i];
//System.out.print(poker[i][j]);
}
//System.out.println();
}
ArrayList<Integer> list = new ArrayList<Integer>(); //创建数组集合
//System.out.println(list);
int index = 0;
HashMap<Integer,String> map = new HashMap<Integer,String>();
for (int i = 0; i < poker.length; i++) {
for (int j = 0; j < poker[i].length; j++) {
map.put(index,poker[i][j]);
list.add(index);
index++;
}
}
Collections.shuffle(list);
TreeSet computer = new TreeSet();
TreeSet person = new TreeSet();
for (int i = 0; i < 6; i++) {
if(i % 2 == 0) {
person.add(list.get(i));
}else {
computer.add(list.get(i));
}
}
System.out.print("person得到的牌: ");
lookPoker(person,map);
System.out.print("computer得到的牌: ");
lookPoker(computer,map);
System.out.println("person的点数:" + getCount(person,map,poker));
System.out.println("computer的点数:" + getCount(computer, map, poker));
compare(getCount(person,map,poker),getCount(computer, map, poker));
}
public static void compare(int person,int computer) {
if (person > computer) {
System.out.println("person获得胜利!");
}
if (person < computer) {
System.out.println("computer获得胜利!");
}
if (person == computer) {
System.out.println("平局!");
}
}
public static int getCount(TreeSet<Integer> ts,HashMap<Integer,String> hm,String[][] poker) {
int count = 0;
for (Integer index : ts) {
count += count(poker,hm.get(index));
}
return count;
}
public static int count(String[][] poker,String str) {
for (int i = 0; i < poker.length; i++) {
for (int j = 0; j < poker[i].length; j++) {
if(poker[i][j].equals(str)) {
return i+1;
}
}
}
return -1;
}
public static void lookPoker(TreeSet<Integer> ts,HashMap<Integer,String> hm) {
for (Integer index : ts ) {
System.out.print(hm.get(index)+ " ");
}
System.out.println();
}
} |
|