我們在課堂上所覆蓋,並可以總結如下的基本遊戲規則。第一輪擲骰子以贏球或輸球結束,或以繼續投擲骰子的要求結束。如果第一輪是7或11,那就是勝利。如果第一卷是2,3或12,那就是虧損。對於任何其他總數(所稱的點數),您必須重新擲出骰子,直到您重複該點數或擲出7爲止。如果重新擲出產生7,則這是一個損失。如果重新拋擲產生這一點,這是一場勝利。如果兩者都不發生,你必須繼續重新研究。
public class CrapsGame{
public static void main(String[] args) {
公共類CrapsGame { 公共靜態無效的主要(字串[] args){
int time; //number of times the game user want to play
int rolls=0; //number of rolls
int mypoint=0;
int d1; //number user rolled for first dice
int d2; //number user rolled for second dice
int total=0; //total number of rolls
int Numwins=0; //number of wins
int Numlosses = 0; //number of losses
double averolls=0; //average number of rolls per game
double probwin=0; //probability of win
boolean win, rollagain=false;
win = false;
System.out.println("Enter the number of times the game you want to play: ");
time=TextIO.getlnInt();
for (int i=0; i<time; i++){
d1=(int)(6*Math.random())+1; //user roll dice(generate a random number)
d2=(int)(6*Math.random())+1;
rolls =0;
switch (d1+d2) {
case 7:
case 11:
win=true; //when first roll is 7 or 11, player win
break;
case 2:
case 3:
case 12:
win=false; //when first roll is 2,3 or 12, player lose
break;
default:
mypoint = d1+d2;
rollagain=true; //when first roll is 1,4,5,6,8,9,10 or 11, player rethrow dice
}
do{
d1=(int)(6*Math.random())+1;
d2=(int)(6*Math.random())+1;
System.out.printf("%d and %d\n",d1,d2);
rolls++;
total = total + rolls;
if (d1+d2 == mypoint){
win=true;
rollagain=false;
}
else if (d1+d2 == 7){
win=false;
rollagain=false;
}
} while(rollagain);
}
if (win){
System.out.printf("***WINNER***\n");
Numwins++;}
else {
System.out.printf("YOU LOSE\n");
Numlosses++;}
probwin = Numwins/time;
averolls = total/time ;
System.out.printf("Avg # rolls: %.2f\n",averolls);
System.out.printf("Max # rolls: %d\n",rolls);
System.out.printf("# of wins: %d\n",Numwins);
System.out.printf("# of losses: %d\n",Numlosses);
System.out.printf("The probability of a win: %.2f.\n",probwin);
}
}Thats all i have so far. My questions are
1.爲什麼該程序沒有計算每場輥,勝數的數的平均值對我來說損失是多少?
2.程序在開始時需要詢問用戶他們想玩的遊戲數量。然後我決定使用for循環,但它說時間無法解析爲變量。 idk發生了什麼事。
3.第一次滾動後,如果用戶第一次滾動相同的enter code here
數字,他們贏得比賽。但我的代碼沒有這樣做。 idk爲什麼。幫助我!
這不是你問的問題,而是你的隨機數生成關閉。比方說,它不會比7更可能。擲2個骰子並添加它們。 –
平均卷數應該是總卷數除以你玩的次數。 –
@Jeremy Kahan說什麼指出了將int rollDie()作爲自己的方法的原因。 –