第六章第三十二题(游戏:赢取双骰子赌博游戏的机会)(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