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.