2017-03-07 35 views
-3

問題java程序要求用戶擲骰子,我這裏有麻煩

我們在課堂上所覆蓋,並可以總結如下的基本遊戲規則。第一輪擲骰子以贏球或輸球結束,或以繼續投擲骰子的要求結束。如果第一輪是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爲什麼。幫助我!

+2

這不是你問的問題,而是你的隨機數生成關閉。比方說,它不會比7更可能。擲2個骰子並添加它們。 –

+0

平均卷數應該是總卷數除以你玩的次數。 –

+0

@Jeremy Kahan說什麼指出了將int rollDie()作爲自己的方法的原因。 –

回答

0

無法正常工作的一個原因是,當您進入默認部分時,您需要設置'point = d'。目前,您將點設置爲零,但您將其作爲目標點進行比較。

原因時間無法解決的是你有它在你的例子中註釋掉。

至於問題1:請注意'total'永遠不會更新。它始終保持爲0.

+0

在默認設置點= d後,它仍然不能工作。 plz再次幫助我 – sing

+0

您需要將rollagain初始化爲false,以便7,11,2,3,12導致不再滾動。另外我非常肯定你想使用一段時間而不是do ... while循環,因爲在這些情況下,循環不應該執行一次。在for循環中的第一行擲骰子之後,您還需要一個擲骰子++。平均分母應該是時間和分子數。 @Oscar是正確的,總是永遠不會增加,而且它應該是時間相同(如此多餘)。 –

+0

我按照你的指示並改變了它。還是行不通。尋求更多的幫助。由於星期三,我現在很恐慌 – sing