第七章第二十九题(游戏:挑选四张牌)(Game: choose four cards)

第七章第二十九题(游戏:挑选四张牌)(Game: choose four cards)

  • *7.29(游戏:挑选四张牌)编写一个程序,从一副52张的牌中选出四张,然后计算他们的和。Ace、King、Queen和Jack分别表示1、13、12和11。程序应该显示得到和为24的选牌次数。
    *7.29(Game: choose four cards)Write a program, from a deck of 52 cards to choose four, and then calculate their sum. Ace, king, Queen, and Jack denote 1, 13, 12, and 11, respectively. The program should show the number of picks that get a sum of 24.

  • 参考代码:

    package chapter07;
    
    public class Code_29 {
        public static void main(String[] args) {
            int[] cards = {1,2,3,4,5,6,7,8,9,10,11,12,13,
                    1,2,3,4,5,6,7,8,9,10,11,12,13,
                    1,2,3,4,5,6,7,8,9,10,11,12,13,
                    1,2,3,4,5,6,7,8,9,10,11,12,13};
            int[] select = new int[4];
            int times = 0;
    
            for(int s1 = 0;s1 < cards.length;s1++){
                for(int s2 = 0;s2 < cards.length;s2++){
                    for(int s3 = 0;s3 < cards.length;s3++){
                        for(int s4 = 0;s4 < cards.length;s4++){
                            select[0] = s1;
                            select[1] = s2;
                            select[2] = s3;
                            select[3] = s4;
    
                            if (select[0] + select[1] + select[2] + select[3] == 24) {
                                times++;
                            }
                        }
                    }
                }
            }
            System.out.println("选牌出现24共有"+times+"次");
        }
    }
    
    
  • 结果显示:

    选牌出现24共有2925次
    
    Process finished with exit code 0
    
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值