第六章第三十题(游戏:双骰子赌博)(Game: craps)

it2025-05-06  10

第六章第三十题(游戏:双骰子赌博)(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参考代码: package chapter06; public class Code_30 { public static void main(String[] args) { int point; int firstDie = rollDie(); int secondDie = rollDie(); int sumOfTwoDice = firstDie + secondDie; if(sumOfTwoDice == 2 || sumOfTwoDice == 3 || sumOfTwoDice == 12) { System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice); System.out.println("You lose"); } else if(sumOfTwoDice == 7 || sumOfTwoDice == 11) { System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice); System.out.println("You win"); } else { point = sumOfTwoDice; System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice); System.out.printf("point is %d\n", point); do { firstDie = rollDie(); secondDie = rollDie(); sumOfTwoDice = firstDie + secondDie; }while(sumOfTwoDice !=7 && sumOfTwoDice != point); System.out.printf("You rolled %d + %d = %d\n",firstDie,secondDie,sumOfTwoDice); if(sumOfTwoDice == point) System.out.println("You win"); else if(sumOfTwoDice == 7) System.out.println("You lose"); } } public static int rollDie() { return (int)(Math.random() * 6 + 1); } } 结果显示: You rolled 4 + 5 = 9 point is 9 You rolled 1 + 6 = 7 You lose Process finished with exit code 0
最新回复(0)