Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a beginner C++ program. I need to print out the percentage of flips that matched the end user's guest. The current line

Need help with a beginner C++ program. I need to print out the percentage of flips that matched the end user's guest. The current line of code does not output anything. I know the math is correct, so maybe it is the placement of the one line of code. Here is what I have so far.

using System;

namespace FlippingCoin { class Program {

static void Main(string[] args) {

int choice; Console.Write("Guess which will have more: heads or tails?"); string headsOrTailsGuess = Console.ReadLine(); if (headsOrTailsGuess.ToLower().Trim() == "heads") { choice = 0; } else { choice = 1; }

Console.Write("How many times shall we flip the coin? "); int numberOfFlips = int.Parse(Console.ReadLine());

// Declare variables Random rand = new Random(); int correctCount = 0; int percentage = (correctCount / numberOfFlips) * 100; //int heads = 0; //int tails = 1;

for (int i = 0; i < numberOfFlips; i++) { int result = rand.Next(0, 2);

if (result == 0) { Console.WriteLine("Heads!"); } else { Console.WriteLine("Tails!"); } if (result == choice) { //comparing result to parsed choice and incrementing the counter correctCount++; }

} Console.WriteLine("Your guess, " + headsOrTailsGuess + " came up " + correctCount + " time(s)."); Console.WriteLine("That's " + percentage + "%"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions