//Craps.java//CrapsClasssimulatesthedicegamecrapsimportjava.util.RandompublicclassCraps{//createrandomnumbergeneratorforuseinmethodrollDice
//Craps.java//CrapsClasssimulatesthedicegamecrapsimportjava.util.RandompublicclassCraps{//createrandomnumbergeneratorforuseinmethodrollDiceprivateenumStatus{CONTINUE,WON,LOST};//contantsthatrepresentcommonrollsofthediceprivatefinalstaticintSNAKE_EYES=2;privatefinalstaticintTREY=3;privatefinalstaticintSEVEN=7;privatefinalstaticintYO_LEVEN=11;privatefinalstaticintBOX_CARS=12;//playonegameofcrapspublicvoidplay(){intmyPoint=0;statusgameStatus;intsumOfDice=rolldice();//firstrollofthedice//determinegamestatusandpointbasedonfirstrollswitch(sumOfDice){:gameStatues=Status.WON;break;caseSNAKE_:caseBOX_:gameStatues=Statues.CONTINUE;myPoint=sumOfDice; intf("pointis%d\n",point);break;}//whilegameisnotcompletewhile(gameStatues==Statues.CONTINUE){sumOfDice=rollDice();//rolldiceagain//determinegamestatusif(sumOfDice==myPoint)gameStatus=status.WON;elseif(sumOfDice==SEVEN)gameStatus=Status.LOST;}//endwhile//displywonorlostmessageif(gameStatues==Status.WON) intln("playerwon");else intln("playerlost");}//endmethodplay//rolldice,calculatesumanddisplayresultspublicintrollDice(){//pickrandomdievaluesintdie1=1+ xtInt(6);intdie2=1+ xtInt(6);intsum=die1+die2; intf("Playerrolled%d+%d=%d\n",die1,die2,sum);returnsum;}//endmethodrollDice}//endClassCraps//Fig.6.10:CrapsTest.java//ApplicationtotestclassCraps.publicclassCrapsTest{publicstaticvoidmain(Stringargs[]){Crapsgame=newCraps(); ay();}//endmain}endclassCrapsTest怎么改才能当程序运行完后问用户是否再玩一遍,当用户输入Y是再运行一遍然后把结果存入一个数组,最后打印一个总结例子:SummaryofGame(s):4 :Lost….
展开
全部