您的位置 首页 Java

第六章第三十题(游戏:双骰子赌博)(Game: craps) - 编程练习题答案

**6.30(游戏:双骰子赌博)执双骰子游戏是赌场中非常流行的骰子游戏。编写程序,玩这个游戏的一个变种,如下所描述:
执两个骰子。每个骰子有六个面,分别表示值1,2,…,6。检查这两个骰子的和。如果和为2、3或12(称为掷骰子(crap)),你就输了;如果和是7或者11(称作自然(natural)),你就赢了;但如果和是其他数字(例如:4、5、6、8、9或者10),就确定了一个点。继续掷骰子,直到掷出一个7或者掷出和刚才相同的点数。如果掷出的是7,你就输了。如果掷出的点数和你前一次掷出的点数相同,你就赢了。程序扮演一个独立的玩家。

下面是一些运行示例:

You rolled 5 + 6 = 11

You win

You rolled 1 + 2 = 3

You lose

You rolled 4 + 4 = 8

point is 8

You rolled 6 + 2 = 8

You win

You rolled 3 + 2 = 5

point is 5

You rolled 2 + 5 = 7

You lose

**6.30(Game: craps)Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows:Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player.

Here are some sample runs.

You rolled 5 + 6 = 11

You win

You rolled 1 + 2 = 3

You lose

You rolled 4 + 4 = 8

point is 8

You rolled 6 + 2 = 8

You win

You rolled 3 + 2 = 5

point is 5

You rolled 2 + 5 = 7

You lose

下面是参考答案代码:

public class Ans6_30_page203 {
    public static void main(String[] args) {
        int guessOne = random(6);
        int guessTwo = random(6);
        int guessThree = 0;
        int sum = guessOne + guessTwo;
        int guessTemp = 0;
        boolean nextGuess = true;

        System.out.println("You rolled "+ guessOne +" + "+ guessTwo + " = "+sum);
        if (sum == 7 || sum == 11)
            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");
                    nextGuess = false;
                } else if (guessThree == guessOne  || guessThree == guessTwo || guessThree
                == guessTemp) {
                    System.out.println("You rolled "+guessThree+" + "+guessThree+" = "+(guessThree*2));
                    System.out.println("You win");
                    nextGuess = false;
                }else {
                    System.out.println("You rolled "+guessThree+" + "+sum+" = "+(guessThree+sum));
                    System.out.println("You lose");
                    break;
                }
                guessTemp = guessThree;
            }
        }
    }
    public static int random(int guess) {
        return 1 + (int)(Math.random()*guess+1);
    }

}

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

未注明转载的文章均为原创,并采用 CC BY-NC-SA 4.0 授权协议,转载请注明来源:https://cn.fankuiba.com/356.html

作者: 博主

发表评论

电子邮件地址不会被公开。 必填项已用*标注

返回顶部