2013-03-09 43 views
0

你好,我正在嘗試創建一個骰子滾動遊戲的頻率表。以下是我正在進行的項目的說明:骰子頻率表c#控制檯

創建一個模擬滾動標準6面模具(編號爲1 - 6)的應用程序。

  • 模具應該滾動10,000次。
  • 萬卷應該是用戶的輸入;詢問他們多久擲一次骰子
  • 擲骰子的值應該根據Random類對象的輸出使用隨機值來確定(請參閱下面的註釋)。
  • 程序完成滾動用戶請求的次數(10,000)後,應用程序應該顯示一個表格,顯示每個骰子滾動的次數。
  • 程序應該詢問用戶他們是否想模擬另一個擲骰子會話。跟蹤會話的數量。

現在我知道如何使用隨機數類,但我堅持對項目彙總表的一部分,我只是需要的東西,可以幫我上手

這裏是我到目前爲止在項目中,正如你會看到我的彙總表是沒有意義的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data; 

namespace Dice 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Random rndGen = new Random(); 

      Console.WriteLine("welcome to the ralph dice game"); 
      Console.Clear(); 

      Console.WriteLine("how many times do you want to roll"); 
      int rollDice = int.Parse(Console.ReadLine()); 

      for (int i = 0; i < rollDice; i++) 
      { 
       int diceRoll = 0; 

       diceRoll = rndGen.Next(1,7); 

       string table = " \tfrequency\tpercent"; 
       table +="\n"+ "\t" + i + "\t" + diceRoll; 

       Console.WriteLine(table); 

      }//end for 

      Console.ReadKey(); 

     } 

    } 
} 

回答

0

目前顯示的次,每次骰子被軋的數量的表格。

如果我理解正確這,這意味着骰子多少次送1,2,3,等等......你需要一個數組來存儲所有的結果計數和輸出時所有輥完成。

注意:未經測試的代碼。

int[] outcomes = new int[6]; 

// init 
for (int i = 0; i < outcomes.Length; ++i) { 
    outcomes[i] = 0; 
} 

for (int i = 0; i < rollDice; i++) 
{ 
    int diceRoll = 0; 

    diceRoll = rndGen.Next(1,7); 

    outcomes[diceRoll - 1]++; //increment frequency. 
    // Note that as arrays are zero-based, the " - 1" part turns the output range 
    // from 1-6 to 0-5, fitting into the array. 

}//end for 

// print the outcome values, as a table 

跟蹤會話的數量。

只是使用另一個變量,但你的代碼顯然似乎沒有這個部分實現。一個簡單的方法是使用一個do-while循環:

do { 

    // your code 

    // ask if user wish to continue 

    bool answer = // if user want to continue 

} while (!answer); 
+0

真的幫了我,但我仍然停留在如何調用從擲骰子是陣列中的這些數字,並把它們放在一個表。 – user2150783 2013-03-11 03:22:51

+0

使用for-loop來讀取數組。 'for(int i = 0; i luiges90 2013-03-11 05:17:07