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

吃可爱长大的小学妹 提交于 2020-07-25 04:44:21

**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.

下面是参考答案代码:

// https://cn.fankuiba.com
public class Ans6_32_page205 {
    public static void main(String[] args) {
        int count = 0;
        for (int c = 0; c < 10000; c++) {
            int guessOne = random(6);
            int guessTwo = random(6);
            int guessThree;
            int sum = guessOne + guessTwo;
            int guessTemp = 0;
            boolean nextGuess = true;

            //System.out.println("You rolled " + guessOne + " + " + guessTwo + " = " + sum);
            if (sum == 7 || sum == 11) {
                count = count + 1;
                //System.out.println("You win");
            } else if (sum == 2 || sum == 3 || sum == 12) {
                //System.out.println("You lose");
            }
            else {
                while (nextGuess) {
                    //System.out.println("point is " + sum);
                    guessThree = random(6);
                    if (guessThree == 7) {
                        //System.out.println("You rolled 7 + " + guessThree + " = " + (guessThree * 2));
                        //System.out.println("You win");
                        count++;
                        nextGuess = false;
                    } else if (guessThree == guessOne || guessThree == guessTwo || guessThree
                            == guessTemp) {
                        //System.out.println("You rolled " + guessThree + " + " + guessThree + " = " + (guessThree * 2));
                        //System.out.println("You win");
                        count++;
                        nextGuess = false;
                    } else {
                        //System.out.println("You rolled " + guessThree + " + " + sum + " = " + (guessThree + sum));
                        //System.out.println("You lose");
                        break;
                    }
                    guessTemp = guessThree;
                }
            }
        }
        System.out.println(count);
    }

    public static int random(int guess) {
        return 1 + (int) (Math.random() * guess + 1);
    }
}

适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)更多内容

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!