Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

package homework1; import java.util.*; public class blackjack { public static void main…

  1. User

    package homework1; import java.util.*; public class blackjack { public static void main (String args[]) { ArrayList had =new ArrayList(); Scanner input =new Scanner(System.in); String col,no; col=input.next(); no=input.next(); hand.add_card(col,no,had); col=input.next(); no=input.next(); hand.add_card(col,no,had); while(hand.ct(had)<17) { System.out.println("Hit"); col=input.next(); no=input.next(); hand.add_card(col,no,had); if(no.equals("A")) System.out.println(col+" 1 11"); else if(no.equals("J")||no.equals("Q")||no.equals("K")) System.out.println(col+" 10"); else System.out.println(col+" "+no);

    	}
    	System.out.println("Stand");
    	hand.sort(had);
    	hand.pout(had);
    	if(hand.ct(had)==21&&had.size()==2) {
    		System.out.print("Blackjack");
    	}
    	else if(hand.ct(had)>21) {
    		System.out.print("Bust");
    	}
    	else
    		System.out.print(hand.ct(had));
    }
    

    } class card implements Comparable{ public String col; public int num; public String nm; public int c; public void set(String co,String nm) { this.col=co; this.nm=nm; switch(nm) { case "A" : this.num=0;this.c=0;break; case "J" : this.num=10;this.c=11;break; case "Q" : this.num=10;this.c=12;break; case "K" : this.num=10;this.c=13;break; default : this.num=Integer.parseInt(nm);this.c=this.num;break; } } public int compareTo(card o) { if(this.c!=o.c) { return this.c-o.c; } else return o.col.compareTo(this.col); } } class hand{ public static void add_card(String co,String nm,ArrayList had) { card x=new card(); x.set(co, nm); had.add(x);

    }
    public static int ct(ArrayList<card> had) {
    	int i,sum,c0;
    	for(i=0,sum=0,c0=0;i<had.size();i++)
    	{
    		if(had.get(i).num==0)
    			c0++;
    		sum=sum+had.get(i).num;
    	}
    	for(i=0;i<c0;i++)
    	{
    		if(sum+11<=21)
    			sum=sum+11;
    		else
    			sum=sum+1;
    	}
    	return sum;
    }
    public static void sort(ArrayList<card> had) {
    	Collections.sort(had);
    }
    public static  void pout (ArrayList<card> had) {
    	int i;
    	for(i=0;i<had.size();i++) {
    		System.out.print(had.get(i).col+had.get(i).nm+" ");
    	}
    	System.out.println();
    }
    

    }21点游戏,有什么问题?

  2. Assistant

    在主函数中的第11行代码有误,应该是hand.add_card(col,no,had);而不是hand.add_card(col,no,hand);。另外,变量hand在主函数中没有定义,应该改成had,与类hand的名字区分开。在类hand中,方法pout和sort应该是静态方法,定义时应该加上static修饰符,这样在主函数中才能直接调用。此外,在方法add_card中,可以直接使用had.add(new card(co, nm));来添加卡牌。最后,在方法card.set中,为了避免magic number,可以用常量替代数字0、10、11、12和13。

  3. User

    并不是这里的问题

  4. Assistant

    经过检查,我发现在方法card.compareTo中比较大小的语句return o.c-this.c;应该改成return this.c-o.c;,才能按照卡牌点数从小到大排序。

  5. User

    不是

  6. Assistant

    既然不是前两次提到的问题,那就请您具体说明一下出现了什么问题,或者给出报错信息或现象。这样才能更好地理解并解决问题。

  7. User

    读入前两张牌不输出,从第三张牌开始(如果需要),则每次要牌,要先输出Hit,然后读入下一张牌,并依次输出该牌的花色及点数(A输出1 11,即它有两个点数)。当不再要牌时要先输出Stand,然后在一行内输出这一手牌,牌与牌之间用一个空格分隔。牌输出的顺序为先看牌面,牌面小的在前(牌面由小到大的顺序为A,2,3....J,Q,K),当牌面相同时看花色,输出顺序从前到后为Spade, Heart, Diamond, Club。最后一行输出这一手牌的结果,如果总点数超过21点,则输出Bust,如果是Blackjack(一手牌只有两张牌且点数相加和为21点),则输出Blackjack。其他情况则输出一个整数,代表这手牌的点数(尽量大且不爆)。具体格式见样例。规则如此

  8. Assistant

    根据您的问题描述,我来分析您的代码:

    import java.util.*;
    
    public class blackjack {
    
        public static void main(String args[]) {
    
            // 创建一个空的 ArrayList 对象来表示当前手牌
            ArrayList<card> hand = new ArrayList<card>();
    
            Scanner input = new Scanner(System.in);
            String col, no;
    
            // 读入前两张牌并加入手牌,但没有输出
            col = input.next();
            no = input.next();
            hand.add(new card(col, no));
    
            col = input.next();
            no = input.next();
            hand.add(new card(col, no));
    
            // 当手牌点数小于 17 时,需要要牌
            while (hand.ct(had) < 17) {
                System.out.println("Hit");
    
                // 读入一张牌并加入当前手牌
                col = input.next();
                no = input.next();
                hand.add(new card(col, no));
    
                // 输出这张新牌的花色和点数
                if (no.equals("A"))
                    System.out.println(col+" 1 11");
                else if (no.equals("J") || no.equals("Q") || no.equals("K"))
                    System.out.println(col+" 10");
                else
                    System.out.println(col+" "+no);
            }
    
            // 不再要牌,输出 Stand,排序并输出当前手牌,然后判断牌型并输出结果
            System.out.println("Stand");
            hand.sort();
            hand.pout();
            int totalPoints = hand.ct();
            if (totalPoints == 21 && hand.size() == 2) {
                System.out.print("Blackjack");
            } else if (totalPoints > 21) {
                System.out.print("Bust");
            } else {
                System.out.print(totalPoints);
            }
    
        }
    
    }
    
    class card implements Comparable<card> {
    
        private String color;
        private int number;
    
        public card(String color, String number) {
            this.color = color;
            this.number = getNumber(number);
        }
    
        private int getNumber(String number) {
            switch (number) {
                case "A":
                    return 1;
                case "J":
                case "Q":
                case "K":
                    return 10;
                default:
                    return Integer.parseInt(number);
            }
        }
    
        @Override
        public int compareTo(card o) {
            if (this.number != o.number) {
                return this.number - o.number;
            } else {
                return this.color.compareTo(o.color);
            }
        }
    }
    
    class hand {
    
        private ArrayList<card> cards;
    
        public hand() {
            this.cards = new ArrayList<>();
        }
    
        public void add(card card) {
            this.cards.add(card);
        }
    
        // 计算当前手牌的点数
        public int ct() {
            int totalPoints = 0;
            int count = 0;
            for (card card : cards) {
                if (card.getNumber() == 1) {
                    count++;
                }
                totalPoints += card.getNumber();
            }
            for (int i = 0; i < count; i++) {
                if (totalPoints + 10 <= 21) {
                    totalPoints += 10;
                }
            }
            return totalPoints;
        }
    
        public void sort() {
            Collections.sort(cards);
        }
    
        public void pout() {
            StringBuilder sb = new StringBuilder();
            for (card card : cards) {
                sb.append(card.getColor()).append(card.getNumber()).append(" ");
            }
            System.out.println(sb.toString().trim());
        }
    
    }
    
    

    经过我的分析,发现代码有以下问题:

    1. 类hand中的 ct、sort 和 pout 方法应该是非静态方法,也就是需要在方法前加上 public void。
    2. 在类card中,成员变量num没有使用到,而getNumber方法中的返回值已经是整型,因此不再需要num属性。同时,为了便于理解,成员变量color应该改名为color。
    3. 在主函数中的第 11 行和第 15 行,应该是 hand.add(new card(col, no)); 而不是 hand.add_card(col, no);。
    4. 在主函数中的循环中,一开始输入了新的一张牌就会立即输出,而应该等到这轮结束后再输出,即把循环中的输出语句移动到循环外。
    5. 当牌点数是 17 到 20 时,不需要要牌也不需要输出"Stand"。
    6. 在类hand的 ct 方法中,当计算 A 的点数时有误,应该先统计 A 的数量,再根据当前总点数来计算。
    7. 在类hand的 ct 方法中,当只有一张牌时并且点数是 A 时,计算 A 的点数时会出现错误,应该根据 A 的数量来计算。
    8. 在类hand的 pout 方法中,末尾的空格会导致输出结果多出一格,应该在前面使用 StringBuilder 进行字符串拼接,最后再使用 trim() 方法去除多余的空格。

    在修改完以上问题后,代码如下所示:

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.