Visual C# 7.29 (Coin Tossing)

This Solution is for the “Coin Tossing” of chapter 6 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/17/2017

/*Description:
App simulates a coin toss. lets user “toss a coin” using menu option. Counts the number of times each side of the
coin appears. Displays the results. Uses a static method flip that takes no arguments and returns False for tails
and True for heads.
*/
using System;

class Program
{
//generates field face from method flip so it is usable in the entire app
private static bool face;

static void Main()
{
string exit;//declares variable exit

//track heads/tails count
int headcount = 0;
int tailscount = 0;

//instructions title
Console.WriteLine(” Game Menu”);
Console.WriteLine(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”);

// do statement causes to loop at least once
do{

//prints game menu and prompts user
Console.WriteLine(“Press <enter> to toss the coin\nOr <ctrl> z to end.”);

exit = Console.ReadLine();//reads for end of file indicator

Console.WriteLine();
int conclusion = flip();//calls method outcome
Console.WriteLine();

//increments for respective count
if (face == true)
{ ++headcount; }
if (face == false)
{ ++tailscount; }

} while (exit != null);// loops until user enters the end of file indicator <Ctrl> z
//before app terminates displays the counts of the number of times each side of the
// coin appeared.
Console.WriteLine(” *****************************************************”);
Console.WriteLine(” * Total number of times the coin landed on Heads: ” + headcount + ” * \n * Total number of times the coin landed on Tails: ” + tailscount +” *”);
Console.WriteLine(” *****************************************************”);
Console.WriteLine();
}

//Uses a satic method flip that takes no arguments and returns False for tails
//and True for heads.
static int flip()
{
//Uses Random object to produce a one-digit integer
Random randomNumbers = new Random();
int coinstatus = randomNumbers.Next(0, 2);//generates random 0 or 1 to represent coin toss

//switch statement changes int 0 or 1 to respective bool true or false
switch (coinstatus)
{
case 0:
face = true;
break;

case 1:
face = false;
break;
}
//Displays the results.
Console.WriteLine($”The coin landed on Heads? ” + face);
return coinstatus;//returns coinstatus to use inside the method
}

}

Program Output:

Game Menu
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? False

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? True

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? False

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? False

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? True

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? True

Press <enter> to toss the coin
Or <ctrl> z to end.
The coin landed on Heads? False

Press <enter> to toss the coin
Or <ctrl> z to end.
^Z

The coin landed on Heads? False

*****************************************************
* Total number of times the coin landed on Heads: 3 *
* Total number of times the coin landed on Tails: 5 *
*****************************************************

Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s