第六章第三十二题(游戏:赢取双骰子赌博游戏的机会)(Game: chance of winning at craps)

第六章第三十二题(游戏:赢取双骰子赌博游戏的机会)(Game: chance of winning at craps)

  • **6.32(游戏:赢取双骰子赌博游戏的机会)修改编程练习题6.30使该程序运行10000次,然后显示赢得游戏的次数
    **6.32(Game: chance of winning at craps)Revise Exercise 6.30 to run it 15,000 times and display the number of winning games.
  • 参考代码:
package chapter06;

public class Code_32 {
    public static void main(String[] args) {
        int sumOfTwoDice, firstDie, secondDie, point, winCount = 0;
        for(int i = 1;i <= 1000;i++) {
            firstDie = rollDie();
            secondDie = rollDie();
            sumOfTwoDice = firstDie + secondDie;
            if (sumOfTwoDice == 2 || sumOfTwoDice == 3 || sumOfTwoDice == 12) {
                winCount++;
            }
            else if (sumOfTwoDice == 7 || sumOfTwoDice == 11) {
            }
            else {
                point = sumOfTwoDice;
                do {
                    firstDie = rollDie();
                    secondDie = rollDie();
                    sumOfTwoDice = firstDie + secondDie;
                } while (sumOfTwoDice != 7 && sumOfTwoDice != point);

                if (sumOfTwoDice == point)
                    winCount++;
            }
        }
        System.out.printf("You totally won %d times", winCount);
    }
    public static int rollDie() {
        return (int) (Math.random() * 6 + 1);
    }
}

  • 结果显示:
You totally won 387 times
Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值