Throw 100 times to calculate the number of times each number appears. C # The source code case is attached to the second simple solution,

Source: Internet
Author: User

Throw 100 times to calculate the number of times each number appears. C # The source code case is attached to the second simple solution,

 
Console. WriteLine ("Throwing 100 experiments:"); // prompt information
Random randomNum = new Random (); // create a Random number
Int num1 = 0; // defines the number of times 1 appears.
Int num2 = 0; // defines the number of times 2 appears.
Int num3 = 0; // defines the number of times 3 appears.
Int num4 = 0; // defines the number of times that 4 appears.
Int num5 = 0; // defines the number of times that 5 appears.
Int num6 = 0; // defines the number of times that 6 appears.


For (int I = 1; I <= 100; I ++) // for statement Loop
{
Int num = randomNum. Next (1, 7); // defines a variable to accept random numbers.

Switch (num)
{
Case 1:
{
Num1 ++;
Break;
}

Case 2:
{
Num2 ++;
Break;
}
Case 3:
{
Num3 ++;
Break;
}
Case 4:
{
Num4 ++;
Break;
}

Case 5:
{
Num5 ++;
Break;
}

Case 6:
{
Num6 ++;
Break;

}
Default:
Break;

}

Console. WriteLine ("the number of {0} Times is: {1}", I, num );
}


Console. WriteLine ("1 appears {0} Times", num1 );
Console. WriteLine ("2 {0} Times", num2 );
Console. WriteLine ("3 {0} Times", num3 );
Console. WriteLine ("4 {0} Times", num4 );
Console. WriteLine ("5 {0} Times", num5 );
Console. WriteLine ("6 {0} Times", num6 );

// Verify if it is 100 times
Int sum = num1 + num2 + num3 + num4 + num5 + num6;
Console. WriteLine (sum );




Solution 2:
Namespaces _ 2011. _ 12. _ 3
{
Class Program
{
Static void Main (string [] args)
{
Int [] count = new int [7];
Random randomNum = new Random ();
Int num = 0;
For (int I = 0; I <100; I ++)
{
Num = randomNum. Next (1, 7 );
Count [num] ++;
}

Console. WriteLine ("1 appears: {0}", count [1]);

Console. WriteLine ("2 appears: {0}", count [2]);

Console. WriteLine ("3: {0}", count [3]);

Console. WriteLine ("4: {0}", count [4]);

Console. WriteLine ("5: {0}", count [5]);

Console. WriteLine ("6 times: {0}", count [6]);


Console. WriteLine ("{0} times in total! ", Count [1] + count [2] + count [3] + count [4] + count [5] + count [6]);
}
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.