Throw 100 throws calculate the number of occurrences of each number C # source case attached to the second simple solution

Source: Internet
Author: User

 
Console.WriteLine ("the experiment was thrown 100 times:");//Prompt Information
Random Randomnum =NewRandom ();//Create a random number
intNUM1 =0;//define the number of occurrences of 1
intnum2 =0;//define the number of occurrences of 2
intNUM3 =0;//define the number of occurrences of 3
intNUM4 =0;//define the number of occurrences of 4
intNUM5 =0;//define the number of occurrences of 5
intNUM6 =0;//define the number of occurrences of 6


for(inti =1; I <= -; i++)//For statement Loop
{
intnum = Randomnum.next (1,7);//define 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 has occurred {0} times", NUM1);
Console.WriteLine ("2 has occurred {0} times", num2);
Console.WriteLine ("3 has occurred {0} times", num3);
Console.WriteLine ("4 has occurred {0} times", NUM4);
Console.WriteLine ("5 has occurred {0} times", NUM5);
Console.WriteLine ("6 has occurred {0} times", NUM6);

//verify whether it is 100 times
intsum = num1 + num2 + num3 + num4 + num5 + num6;
Console.WriteLine (sum);




The second method of solution:
namespace_2011._12._3
{
classProgram
{
Static voidMain (string[] args)
{
int[] Count =New int[7] ;
Random Randomnum =NewRandom ();
intnum =0;
for(inti =0; I < -; i++)
{
num = Randomnum.next (1,7);
Count[num] + +;
}

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

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

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

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

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

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


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




Throw 100 throws calculate the number of occurrences of each number C # source case attached to the second simple solution

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.