2.扑克牌使用集合完成
3.方法调用
*/
import java.util.*;
class Test8
{
public static void main(String[] args)
{
//得到扑克牌
List<Poker> list=PokerUtil.getPokers();
PokerUtil.xipai(list);
PokerUtil.showPokers(list);
System.out.println("----------------------------------");
Poker p=new Poker("10","红桃");
int index=PokerUtil.findPoker(list,p);
if(index==-1){
System.out.println("没有这张扑克牌 "+p);
}else{
System.out.println(p+" 在"+(index+1)+"位置");
}
}
}
//声明扑克类
class Poker
{
String number;
String color;
public Poker(){}
public Poker(String number ,String color){
this.number=number;
this.color=color;
}
public boolean equals(Object obj){
Poker p=(Poker)obj;
return p.number.equals(number)&&p.color.equals(color);
}
public String toString(){
return color+number;
}
}
//扑克牌工具类
class PokerUtil
{
public static final String[] ns={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public static final String[] cs={"黑桃","红桃","方块","梅花"};
//查找某张牌
public static int findPoker(List<Poker> list,Poker p){
int n1=ran.nextInt(list.size()); //0-52
int n2=ran.nextInt(list.size());
Poker p1=list.get(n1);
Poker p2=list.get(n2);
//将n1与n2位置上的元素进行交换.
list.set(n1,p2);
list.set(n2,p1);
}
}
//显示扑克
public static void showPokers(List<Poker> list){