当前位置: 代码迷 >> Java相关 >> 一个集合的题目,我第四问不会做,求帮忙啊!
  详细解决方案

一个集合的题目,我第四问不会做,求帮忙啊!

热度:321   发布时间:2013-03-18 18:27:23.0
一个集合的题目,我第四问不会做,求帮忙啊!
package com.chinasoft.pai;
//编程课题:利用Collection提供的排序,搜索,洗牌方法编写一个程序使其具有以下功能:
//(1)对一副扑克牌洗牌。(2)按照红桃,黑桃,梅花,方块排序。
//(3)按照2或4人分牌,并显示每人的牌。(4)查询是否有一对牌以及他在牌中的位置。
//这个第四问怎么做啊??求解。
import java.util.*;
public class Xipai {
    public static void main(String[] args){
    String[] paiList=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    String[] huase=new String[]{"红桃","黑桃","梅花","方块"};
    String[] paiz=new String[52];
    String[] shit=new String[52];
    int j=0;
    int r=0;
    int e=0;
    for(int i=0;i<huase.length;i++){
        for(int o=0;o<paiList.length;o++){            
                paiz[r]=huase[i]+"   "+paiList[o];
                ++r;
                }
        }
    for(int i=0;i<paiz.length;i++)    //以下是对上面字符串数组的按花色排序
    {
        if(paiz[i].substring(0, 2).equals("红桃"))
        {
            shit[j]=paiz[i];
            j++;
        }
    }
    for(int i=0;i<paiz.length;i++)
    {
        if(paiz[i].substring(0, 2).equals("黑桃"))
        {
            shit[j]=paiz[i];
            j++;
        }
    }
    for(int i=0;i<paiz.length;i++)
    {
        if(paiz[i].substring(0, 2).equals("方块"))
        {
            shit[j]=paiz[i];
            j++;
        }
    }
    for(int i=0;i<paiz.length;i++)
    {
        if(paiz[i].substring(0, 2).equals("梅花"))
        {
            shit[j]=paiz[i];
            j++;
        }
    }
    for(int k=0;k<shit.length;k++)    //以下是将原来的数组赋值到新的数组中,实现排序的输出
    {
        System.out.print(shit[k]+" ");
    }
    System.out.println();
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    List<String> pai= Arrays.asList(shit);    //以下三行是将数组转换成集合,并调用洗排方法洗排
    Collections.shuffle(pai);
    System.out.println("洗牌后的结果为:\n"+pai);
    //以下 三行为分排实现
    System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$---将排分给二个人---$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println("第一个人分到的排:\n"+pai.subList(0,26));
        System.out.println("第二个人分到的排:\n"+pai.subList(26,52));
    //查询是否有相同的排?
    }
    }
搜索更多相关的解决方案: package  public  import  

----------------解决方案--------------------------------------------------------
学习了
----------------解决方案--------------------------------------------------------
怎么没有人回复啊


----------------解决方案--------------------------------------------------------
程序代码:
import java.util.*;
public class Xipai {
   

    /**
     * 用于输出显示
     *
@param name
     *
@param list
     
*/
    public static void display(String name,List<Pai> list){
        StringBuilder sb=new StringBuilder();
        sb.append(name).append(":");
        Iterator<Pai> iter=list.iterator();
        while(iter.hasNext()){
            sb.append(iter.next().toString());
            if(iter.hasNext()){
                sb.append(",");
            }
        }
        System.out.println(sb.toString());
    }
   

    /**
     *
@param args
     
*/
    public static void main(String[] args) {
        //(1)对一副扑克牌洗牌。(2)按照红桃,黑桃,梅花,方块排序。
        
//(3)按照2或4人分牌,并显示每人的牌。(4)查询是否有一对牌以及他在牌中的位置。
        System.out.println("=========生成牌==============");
        List<Pai> list=Pai.create();
        display("显示:", list);
        System.out.println("=========洗牌==============");
        Collections.shuffle(list);
        display("显示:", list);
        System.out.println("=========按花色排序==============");
        Collections.sort(list, new Comparator<Pai>() {

            @Override
            public int compare(Pai o1, Pai o2) {
                int x=(o1.getHuase()+1)*50+(o1.getShuzhi()+1);
                int y=(o2.getHuase()+1)*50+(o2.getShuzhi()+1);
                return x-y;
            }
           

        });
        display("显示:", list);
        System.out.println("=========洗牌并发牌==============");
        Collections.shuffle(list);
        List<Pai> list1=list.subList(0, 2*13);
        List<Pai> list2=list.subList(2*13, 4*13);
        display("甲:", list1);
        display("乙:", list2);
        System.out.println("=========找牌==============");
        List<Pai> list3=new ArrayList<Pai>(list1);
        List<Pai> list4=new ArrayList<Pai>(list2);
        Comparator<Pai> com=new Comparator<Pai>() {
            @Override
            public int compare(Pai o1, Pai o2) {
                int x=o1.getShuzhi();
                int y=o2.getShuzhi();
                return x-y;
            }
        };
        Collections.sort(list3, com);
        Collections.sort(list4, com);
        Stack<Pai> stack=new Stack<Pai>();
        System.out.println("=========甲==========");
        for (Pai pai : list3) {
            if(stack.empty()){
                stack.push(pai);
            }else if(stack.peek().getShuzhi().equals(pai.getShuzhi())){
                stack.push(pai);
            }else{
                if(stack.size()==2){
                    Pai p1=stack.pop();
                    Pai p2=stack.pop();
                    System.out.println("一对:"+p1.showShuzhi()+"  位置:第"+(list1.indexOf(p1)+1)+"张,第"+(list1.indexOf(p2)+1)+"张");
                }
                stack.clear();
                stack.push(pai);
            }
        }
        if(stack.size()==2){
            Pai p1=stack.pop();
            Pai p2=stack.pop();
            System.out.println("一对:"+p1.showShuzhi()+"  位置:第"+(list1.indexOf(p1)+1)+"张,第"+(list1.indexOf(p2)+1)+"张");
        }
        System.out.println("=========乙==========");
        stack.clear();
        for (Pai pai : list4) {
            if(stack.empty()){
                stack.push(pai);
            }else if(stack.peek().getShuzhi().equals(pai.getShuzhi())){
                stack.push(pai);
            }else{
                if(stack.size()==2){
                    Pai p1=stack.pop();
                    Pai p2=stack.pop();
                    System.out.println("一对:"+p1.showShuzhi()+"  位置:第"+(list2.indexOf(p1)+1)+"张,第"+(list2.indexOf(p2)+1)+"张");
                }
                stack.clear();
                stack.push(pai);
            }
        }
        if(stack.size()==2){
            Pai p1=stack.pop();
            Pai p2=stack.pop();
            System.out.println("一对:"+p1.showShuzhi()+"  位置:第"+(list2.indexOf(p1)+1)+"张,第"+(list2.indexOf(p2)+1)+"张");
        }
        stack.clear();
    }
}

class Pai{
    private static String[] paiList = new String[] { "2", "3", "4", "5", "6", "7", "8",
            "9", "10", "J", "Q", "K", "A" };
    private static String[] huaselist = new String[] { "红桃", "黑桃", "梅花", "方块" };
    private Integer huase;
    private Integer shuzhi;
   

    /**
     * 创建4*13张牌
     *
@return
     
*/
    public static List<Pai> create(){
        List<Pai> list=new ArrayList<Pai>(4*13);
        for (int i = 0; i < paiList.length; i++) {
            for (int j = 0; j < huaselist.length; j++) {
                list.add(new Pai(j, i));
            }
        }
        return list;
    }
   

    public String showShuzhi() {
        return paiList[shuzhi];
    }

    public Integer getHuase() {
        return huase;
    }
    public void setHuase(Integer huase) {
        this.huase = huase;
    }
    public Integer getShuzhi() {
        return shuzhi;
    }
    public void setShuzhi(Integer shuzhi) {
        this.shuzhi = shuzhi;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((huase == null) ? 0 : huase.hashCode());
        result = prime * result + ((shuzhi == null) ? 0 : shuzhi.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Pai other = (Pai) obj;
        if (huase == null) {
            if (other.huase != null)
                return false;
        } else if (!huase.equals(other.huase))
            return false;
        if (shuzhi == null) {
            if (other.shuzhi != null)
                return false;
        } else if (!shuzhi.equals(other.shuzhi))
            return false;
        return true;
    }
    public Pai(Integer huase, Integer shuzhi) {
        super();
        this.huase = huase;
        this.shuzhi = shuzhi;
    }
    @Override
    public String toString() {
        return huaselist[huase]+" "+paiList[shuzhi]+" ";
    }
   

   

}

----------------解决方案--------------------------------------------------------
楼上写的不错,谢谢了,给分。
Stack<Pai> stack=new Stack<Pai>();和list2.indexOf(p1)+1)用的太亮了!!!



----------------解决方案--------------------------------------------------------
版主代码能力真好。
----------------解决方案--------------------------------------------------------
Collections.sort(list, new
Comparator<Pai>() {
@Override
public int compare(Pai o1,
Pai o2) {
int x=(o1.getHuase
()+1)*50+(o1.getShuzhi()+1);
int y=(o2.getHuase
()+1)*50+(o2.getShuzhi()+1);
return x-y;
}
});四楼,求解释啊。
----------------解决方案--------------------------------------------------------
  相关解决方案