2010/04/13

Exam preparation C# Roll die

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


 

namespace ConsoleApplication3

{


//Roll die programming


//This example is that someone throws a roll die 500 times.


//And we count how many times each face is shown.


class
Program

{


static
void Main(string[] args)

{


//Initialize Random generator


Random rd = new
Random();


 


//Initialize variables for each face


int count1=0;


int count2=0;


int count3=0;


int count4=0;


int count5=0;


int count6=0;


 


 


//A roll die is thrown 500 times and count each face


for (int i = 0; i <500; i++)

{


//Create random number


int x = 1+rd.Next(6);


 


//Use swich to count each face


switch (x)

{


//Add the count of face 1


case 1:

count1++;


break;


//Add the count of face 2


case 2:

count2++;


break;


//Add the count of face 3


case 3:

count3++;


break;


//Add the count of face 4


case 4:

count4++;


break;


//Add the count of face 5


case 5:

count5++;


break;


//Add the count of face 6


case 6:

count6++;


break;

}

}


//Show total counts in each face


Console.WriteLine("face 1 is " + count1);


Console.WriteLine("face 2 is " + count2);


Console.WriteLine("face 3 is " + count3);


Console.WriteLine("face 4 is " + count4);


Console.WriteLine("face 5 is " + count5);


Console.WriteLine("face 6 is " + count6);

}

}

}

0 件のコメント:

コメントを投稿

UA-9417263-1