C # console application to simulate coin throwing Process

Source: Internet
Author: User

First, create a new console application in vs2005:
The Application name is playcoin.
(1) complete code for coin throwing is as follows:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace playcoin
{
Class Program
{
Private int negative = 0;
Private int position = 0;
Private int turn;
Public Program (INT turn)
{
This. Turn = turn;
}
Public void tossing ()
{
Random Rand = new random ();
For (INT I = 0; I <turn; I ++)
{
Getresult (RAND. Next (2 ));
}
}
Public void getresult (int I)
{
If (I = 0)
Negative ++;
Else if (I = 1)
Position ++;
Else
Console. writeline ("incorrect result! ");
}
Public void showresults ()
{
Console. writeline ("number of coins thrown: {0}", turn );
Console. writeline ("number of front displays: {0}", negative );
Console. writeline ("probability of positive appearance: {0} %", (float) Negative/(float) turn * 100 );
Console. writeline ("reverse appearance times: {0}", position );
Console. writeline ("reverse Probability: {0} %", (float) position/(float) turn * 100 );
}
Public void setnumber ()
{
Int J;
Console. writeline ("Enter the number 1 to start throwing :");
J = convert. toint32 (console. Readline ());
If (j = 1)
{
Console. writeline ("Enter the number of coin throwing times :");
Turn = convert. toint32 (console. Readline ());
Tossing ();
Console. writeline ("Enter number 2 to display the throwing Result :");
J = convert. toint32 (console. Readline ());
}
If (j = 2)
{
Showresults ();
}
}
Static void main (string [] ARGs)
{
Program Co = new program (0 );
CO. setnumber ();
Console. Read ();
}
}
}
(2) run the program (Press F5 ):
Enter number 1 to start coin throwing:
1 (enter key)
Enter the number of coin throwing times:
888 (enter key)
Enter number 2 to display the throwing result:
2 (enter key)
Number of times a coin is thrown: 888
Number of front displays: 446
Probability of positive appearance: 50.22522%
Number of reverse appearances: 442
Negative Probability: 49.77478%
(Enter key) to end the program.
(3) Description:
This program focuses on algorithm implementation.

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.