有人能解释清楚吗?我在这里粘贴了相关的Java代码。我在想试验的次数和下注的次数是一样的。
public class Gambler {
public static void main(String[] args) {
int stake = Integer.parseInt(args[0]); // gambler's stating bankroll
int goal = Integer.parseInt(args[1]); // gambler's desired bankroll
int T = Integer.parseInt(args[2]); // number of trials to perform
int bets = 0; // total number of bets made
int wins = 0; // total number of games won
// repeat T times
for (int t = 0; t < T; t++) {
// do one gambler's ruin simulation
int cash = stake;
while (cash > 0 && cash < goal) {
bets++;
if (Math.random() < 0.5) cash++; // win $1
else cash--; // lose $1
}
if (cash == goal) wins++; // did gambler go achieve desired goal?
}
// print results
System.out.println(wins + " wins of " + T);
System.out.println("Percent of games won = " + 100.0 * wins / T);
System.out.println("Avg # bets = " + 1.0 * bets / T);
}
}
在您的代码示例中,程序正在运行赌博游戏。当玩家达到一定数量的金钱("目标"变量)或为零时,游戏便会结束。该程序一直跟踪下注的金额,直到资金枯竭或达到目标。这是变量'bets'或投注的数量。
游戏重复几次,用变量T(试验次数)表示。在每次试验中,该程序都会记录下注总额(跨试验)。
最后,程序计算投注的平均金额。也就是说,在玩这个游戏x次之后,平均每场需要这么多赌注