扑克牌比较大小、扑克牌排序(Java集合框架)

1.2.8 编写一个扑克类(每张扑克有花色和数值,例如红心A,草花10),将52张扑克牌放到集合中(自己去选择集合类),编写3个方法,并测试方法

 方法1.将集合中的所有扑克重新洗牌,并将扑克输出(打印出来即可)

 方法2:比较两张扑克牌的大小,如果版面的值相等,则按(黑桃->红心->草花->方块)的顺序进行排列

 方法3.将集合中的所有扑克排序(按方块3~方块2,草花3~草花2, 红心3~红心2, 黑桃3~黑桃2的顺序排序)

用到的工具:Comparator接口,Comparable接口,Collections工具类\ArrayList集合

package com.oracle.test8;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Poker implements Comparable<Poker>{
	private String color;
	private String num;
	public String getColor() {
		return color;
	}
	public String getNum() {
		return num;
	}
	Poker(String color,String num){
		this.color=color;
		this.num=num;
	}
	public String toString(){
		return color+num;
	}
	
	public static ArrayList<Poker> createPokerList(){
		ArrayList<Poker> list=new ArrayList<Poker>();
		String[] colors={"黑桃","红心","草花","方块"};
		String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		for(String color:colors){
			for(String num:nums){
				list.add(new Poker(color,num));
			}
		}
		return list;
	} 
	public static ArrayList<Poker> shuffle(ArrayList<Poker> arr){
		Collections.shuffle(arr);
		return arr;
	}
	@Override
	public int compareTo(Poker p) {
		String colororder="黑桃,红心,草花,方块";
		String numorder="3,4,5,6,7,8,9,10,J,Q,K,A,2";
		int x=numorder.indexOf(this.getNum());
		int y=numorder.indexOf(p.getNum());
		if(x<y){
			return -1;
		}else if(x>y){
			return 1;
		}else{
			x=colororder.indexOf(this.getColor());
			y=colororder.indexOf(p.getColor());
			if(x<y){
				return 1;
			}else if(x>y){
				return -1;
			}
		}
		return 0;
	}
	public static void main(String[] args) {
		ArrayList<Poker> pokerlist=shuffle(createPokerList());
		for(Poker p:pokerlist){
			System.out.println(p.getColor()+p.getNum());
		}
		Poker p1=pokerlist.get(new Random().nextInt(pokerlist.size()));
		Poker p2=pokerlist.get(new Random().nextInt(pokerlist.size()));
		if(p1.compareTo(p2)>0){
			System.out.println(p1+">"+p2);
		}else if(p1.compareTo(p2)<0){
			System.out.println(p1+"<"+p2);
		}else{
			System.out.println(p1+"="+p2);
		}
		System.out.println("-------------------");
		Collections.sort(pokerlist,new PokerSort());
		for(Poker p:pokerlist){
			System.out.println(p.getColor()+p.getNum());
		}
	}
}
 PokerSort.java

package com.oracle.test8;

import java.util.Comparator;

public class PokerSort implements Comparator<Poker>{

	@Override
	public int compare(Poker p1, Poker p2) {
		String colororder="黑桃,红心,草花,方块";
		String numorder="3,4,5,6,7,8,9,10,J,Q,K,A,2";
		int c1=colororder.indexOf(p1.getColor());
		int c2=colororder.indexOf(p2.getColor());
		int n1=numorder.indexOf(p1.getNum());
		int n2=numorder.indexOf(p2.getNum());
		if(c1==c2){
			if(n1>n2) return 1;
			if(n1<n2) return -1;
			else return 0;
		}else if(c1<c2){
			return 1;
		}else if(c1>c2){
			return -1;
		}
		return 0;
	}
}
 参考文章:http://blog.csdn.net/bazingaea/article/details/50886513


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值