2017-03-15 33 views
-1
import java.util.Random; 

public class diceSimulation { 

private static Random rollDice = new Random(); 

public static void main(String[] args) { 

    final int ROLLCOUNT = 100000; 
    final int SIDES = 12; 
    int count, dice1, dice2; 


    int [] doubleCounts = new int [SIDES]; 


    welcome(); 


    for (count=1; count<=ROLLCOUNT;count++){ 

     dice1=roll(SIDES); 
     dice2=roll(SIDES); 

     if (dice1==dice2){ 
      doubleCounts[dice1-1]++; 
     } 

    } 

    // Display results totals of paired rolls 
    for (int idx=0; idx<doubleCounts.length; idx++){ 
     System.out.format(" You rolled set of %d %d times\n",(idx+1), doubleCounts[idx]); 
    } 
} 

private static int roll(int sides){ 
    return rollDice.nextInt(sides) + 1; 
} 

public static void welcome() { 
    System.out.println("Here are your rolling dice percentages"); 
} 
} 

希望這是我的地方,我應該檢查多少次擲骰的每側使用12個邊,而對一些骰子的模擬項目代碼原因我的代碼輸出只給了我數以百計,不加起來是10,000的數字,這是rollount。有人能爲我解決這個問題嗎?我很抱歉,如果我的代碼是壞的,但我是新的編碼和堆棧溢出,所以請讓我知道,如果有什麼我可以修復。爲什麼我的輸出進來數百當我在數千

+0

按照慣例,程序員使用'for(count = 0; count awiebe

+0

感謝它現在更好的工作,但仍然沒有加起來10000 – JohnYohana

+0

你能給我們一個樣本輸出和你想要的樣本嗎? –

回答

-1

如果你只關心雙數,滾動雙打的可能性比100000,12面的骰子顯著少,所以你不應該指望雙打計數當您滾動兩個骰子總和爲100000

+0

哦,但有沒有一種方法可以使輸出增加到10000? – JohnYohana

+0

爲什麼downvote?答案與下面基本相同。 – awiebe

+0

這是一個意外,我怎麼投票呢? – JohnYohana

1

,你得到一對的概率只是16.7%,所以如果你滾動100000次,你可能會得到16700對。我相信你的代碼正在如何工作。

+0

謝謝你先生它的工作 – JohnYohana

+0

@JohnYohana你做了什麼?你是如何修復它的? –

+0

其實我並沒有想出另外一個問題,我要拍攝我的屏幕截圖,並向你展示問題 – JohnYohana

相關問題