求大神给出扑克牌插入排序的伪码及其改进后的JAVA代码

vmajndzz 2020-07-09 11:26:17
求大神给出扑克牌插入排序的伪码及其改进后的JAVA代码
...全文
2389 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
胖到没有朋友 2020-07-10
  • 打赏
  • 举报
回复
运行结果: 已经为你随机生成10张扑克牌,当前扑克牌的顺序为: Poker{point='A', decor='♦'} Poker{point='2', decor='♥'} Poker{point='2', decor='♣'} Poker{point='4', decor='♠'} Poker{point='6', decor='♠'} Poker{point='7', decor='♥'} Poker{point='7', decor='♠'} Poker{point='8', decor='♦'} Poker{point='8', decor='♠'} Poker{point='Q', decor='♦'} ---------------------- 请输入点数: 4 请输入花色: a 输入的花色不合法请重新输入: 请输入花色: ♠ 当前已经在该张牌: 请输入点数: 4 请输入花色: ♣ 插入成功,当前扑克牌的顺序为: Poker{point='A', decor='♦'} Poker{point='2', decor='♥'} Poker{point='2', decor='♣'} Poker{point='4', decor='♠'} Poker{point='4', decor='♣'} Poker{point='6', decor='♠'} Poker{point='7', decor='♥'} Poker{point='7', decor='♠'} Poker{point='8', decor='♦'} Poker{point='8', decor='♠'} Poker{point='Q', decor='♦'} 请输入点数:
胖到没有朋友 2020-07-10
  • 打赏
  • 举报
回复
Poker对象代码

public class Poker {
    String point;//点数
    Character decor;//花色

    public Poker(String point, Character decor) {
        this.point = point;
        this.decor = decor;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public Character getDecor() {
        return decor;
    }

    public void setDecor(Character decor) {
        this.decor = decor;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "point='" + point + '\'' +
                ", decor='" + decor + '\'' +
                '}';
    }
}
胖到没有朋友 2020-07-10
  • 打赏
  • 举报
回复

private static final String[] pokerArray = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    private static final char[] decorArray = {'♥','♦','♠','♣'};

    /**
     * 1.初始化有序数组,
     * 2.提示用户输入对象属性,校验数据的合法性
     * 3.获取该对象应当插入的下标 :
     *    i.如果当前对象的点数小于列表中某一对象(list.get(i))的点数,说明当前对象应当插入在某一对象(list.get(i))之前
     *    ii.如果当前对象的点数的等于列表中某一对象(list.get(i))的点数,比较花色的大小,如果花色小于某一对象(list.get(i))的点数,说明当前对象应当插入在某一对象之前
     *    iii.如果遍历结束都没有找到下标,说明该对象大于当前列表中所有对象,直接插入在最后面即可
     */

    public static void main(String[] args) {
        ArrayList<Poker> list = initArrayList();;
        System.out.println("已经为你随机生成10张扑克牌,当前扑克牌的顺序为:");
        list.forEach(System.out::println);
        Scanner sc = new Scanner(System.in);
        System.out.println("----------------------");
        while (true){
            String point = "";
            do{
                try {
                    System.out.println("请输入点数:");
                    point = sc.nextLine();
                    String finalPoint = point;
                    if (Arrays.stream(pokerArray).noneMatch(p -> p.equals(finalPoint))){
                        System.out.println("输入的点数不合法请重新输入:");
                        //置空,继续进入循环
                        point = "";
                    }
                }catch (Exception e){
                    System.out.println("输入的点数未识别到,请重新输入:");
                }
            }while ("".equals(point));
            char decor = ' ';
            do{
                try {
                    System.out.println("请输入花色:");
                    decor = sc.nextLine().charAt(0);
                    boolean check = false;
                    for (char c : decorArray) {
                        if (c == decor) {
                            check = true;
                            break;
                        }
                    }
                    if (!check){
                        System.out.println("输入的花色不合法请重新输入:");
                        decor = ' ';
                    }
                }catch (Exception e){
                    System.out.println("输入的花色未识别到,请重新输入:");
                }
            }while (decor ==' ');
            Poker p = new Poker(point,decor);
            int index = getIndex(list,p);
            if (index != -1){
                list.add(index,p);
                System.out.println("插入成功,当前扑克牌的顺序为:");
                list.forEach(System.out::println);
            }else {
                System.out.println("当前已经在该张牌:");
            }
        }

    }

    private static ArrayList<Poker> initArrayList() {
        ArrayList<Poker> list = new ArrayList<>();
        do {
            Poker p = new Poker(pokerArray[(int) (Math.random()*13)],decorArray[(int) (Math.random()*4)]);
            int index = getIndex(list,p);
            if (index != -1){
                //牌重复,不添加
                list.add(index,p);
            }
        }while (list.size()<10);
        return list;
    }

    private static int getIndex(ArrayList<Poker> list, Poker p) {
        if (list.size() == 0){
            return 0;
        }
        for (int i = 0; i < list.size(); i++) {
            //点数小于已排序列表中的第i个对象,说明要插入的位置就在这个元素之前
            if (getPointNum(p) < getPointNum(list.get(i))){
                return i;
            }
            if (getPointNum(p) == getPointNum(list.get(i))){
                //点数小于相等 花色小于已排序列表中的第i个对象,也说明要插入的位置就在这个元素之前
                if (getDecorNum(p) < getDecorNum(list.get(i))){
                    return i;
                }
                //如果花色相等,说明重复
                if (getDecorNum(p) == getDecorNum(list.get(i))){
                    return -1;
                }
            }
        }
        //遍历完说明最新的排大于任何一个元素
        return list.size();
    }


    private static int getPointNum(Poker p ){
        int point = 0;
        for(int i = 0;i<pokerArray.length;i++){
            if (pokerArray[i].equals(p.getPoint())){
                point = i+1;
            }
        }
        return point;
    }

    private static int getDecorNum(Poker p ){
        int decor = 0;
        for(int i = 0;i<decorArray.length;i++){
            if (decorArray[i]==p.getDecor()){
                decor = i+1;
            }
        }
        return decor;
    }

50,532

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧