2010/03/26

C# week9_2

Class Program

  1. Throw roll die 500 times and count how many times each face is shown

    namespace ConsoleApplication22

    {

    class Program

    {

    static void Main(string[] args)

    {

    int counter1 = 0;

    int counter2 = 0;

    int counter3 = 0;

    int counter4 = 0;

    int counter5 = 0;

    int counter6 = 0;

    Random r = new Random();

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

    {

    int num = r.Next(1,7);

     
     

    switch(num){

    case 1:

    counter1++;

    break;

    case 2:

    counter2++;

    break;

    case 3:

    counter3++;

    break;

    case 4:

    counter4++;

    break;

    case 5:

    counter5++;

    break;

    case 6:

    counter6++;

    break;

    }

    }

     
     

    Console.WriteLine("The sum of 1 face " + counter1);

    Console.WriteLine("The sum of 2 face " + counter2);

    Console.WriteLine("The sum of 3 face " + counter3);

    Console.WriteLine("The sum of 4 face " + counter4);

    Console.WriteLine("The sum of 5 face " + counter5);

    Console.WriteLine("The sum of 6 face " + counter6);

    }

    }

    }

     
     

  2. Throw roll die 6times and counts sum up them. Repeat this action 500times and count how many times total becomes 10.

    namespace ConsoleApplication22

    {

    class Program

    {

    static void Main(string[] args)

    {

     
     

    int counter = 0;

    Random r = new Random();

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

    {

    int sum = 0;

    for (int j = 0; j < 6; j++)

    {

    sum = sum+r.Next(1,6);

     
     

     
     

    }

    if (sum == 10)

    {

    counter++;

    }

    }

     
     

    Console.WriteLine("The sum of all faces"+counter);

    }

    }

    }

0 件のコメント:

コメントを投稿

UA-9417263-1