/* The Saint Petersburg Game is a hypothetical casino game played by two players
* I begin by putting $1 on the table, and you then repeatedly flip a coin until it comes up tails.
* Each time the coin comes up heads, I double the amount of money on the table.
* As soon as the coin comes up tails, the game is over and you win all the money.
* program that plays the Saint Petersburg Game as many times as is necessary to earn a total of at least $20.
*/
import acm.program.*;
import acm.util.RandomGenerator;
public class SaintPetersburgGame extends ConsoleProgram {
/** The total number*/
public static double total = 0;
public void run() {
//when total id less than 20,repeat the game;
while(total<=20){
playGame();
}
}
private void playGame() {
//play a game until this turn comes up tail,then plus the number to total.
Boolean comesUpHead = true;
int num = 0;
while(comesUpHead){
RandomGenerator rgen = RandomGenerator.getInstance();
comesUpHead = rgen.nextBoolean();
num++;
}
println("This game, you earned $"+Math.pow(2, num)+".");
total = total + Math.pow(2, num);
println("Your total is $"+total+".");
}
}
CS106A assignment3 --problem5 The Saint Petersburg Game
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門粉臊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驶兜,你說我怎么就攤上這事扼仲。” “怎么了抄淑?”我有些...
- 文/不壞的土叔 我叫張陵屠凶,是天一觀的道長。 經(jīng)常有香客問我肆资,道長矗愧,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任郑原,我火速辦了婚禮唉韭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘犯犁。我一直安慰自己属愤,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布栖秕。 她就那樣靜靜地躺著春塌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上只壳,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼远搪!你這毒婦竟也來了劣纲?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布净蚤,位于F島的核電站,受9級特大地震影響输硝,放射性物質(zhì)發(fā)生泄漏今瀑。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一点把、第九天 我趴在偏房一處隱蔽的房頂上張望橘荠。 院中可真熱鬧,春花似錦郎逃、人聲如沸哥童。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽贮懈。三九已至匀泊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間朵你,已是汗流浹背各聘。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- https://hbr.org/product/right-game-use-game-theory-to-sha...
- 前些日子水孩,很難得的去瀏覽游戲新聞的時候镰矿,不經(jīng)意間發(fā)現(xiàn)最終幻想10和最終幻想10-2已經(jīng)出了PC中文高清重置版。于是...
- I am upset that I can't do anything for my partner and ou...
- 晚上吃完晚飯荷愕,因為霧霾的原因(450多)衡怀,沒有怎么去散步,很快就回家了安疗。只有不到200米的路,也和文文嬉笑走著够委,不...