Find the best solutions to your questions at Westonci.ca, the premier Q&A platform with a community of knowledgeable experts. Experience the convenience of finding accurate answers to your questions from knowledgeable professionals on our platform. Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform.
Sagot :
Answer:
public class CoinFlips extends ConsoleProgram
{
public static final int FLIPS = 100;
public void run()
{
int countH = 0;
int countT = 0;
for(int i = 0; i < 100; i++)
{
if (Randomizer.nextBoolean())
{
System.out.println("Heads");
countH += 1;
}
else
{
System.out.println("Tails");
countT += 1;
}
}
System.out.println("Heads: " + countH);
System.out.println("Tails: " + countT);
System.out.println("% Heads: " + (double) countH / FLIPS);
System.out.println("% Tails: " + (double) countT / FLIPS);
}
}
Explanation:
First define your counting variables for both heads and tails (I named them countH and countT). Set them to 0 at the start of the run.
Then use a for loop to flip the coin 100 times. In the video you should have learned about the Randomizer class so you can use the same idea to print out whether you got heads or tails.
Make sure to keep the count going using >variable name< += 1.
The printing at the end is very basic; print the statement for each: ("Heads: " + >variable name<);
For the percentages, print ("% Heads: " + (double) >variable name< / FLIPS); divided by FLIPS (not 100 or any other int because you will get the wrong value) and remember to cast them as doubles to get the correct value.
The program simulates 100 coin flips and displays the result of each flip and the resulting percentage. The program written in python 3 goes thus :
import random
#import the random module
total = 0
#initialize the total coin flips
h_count = 0
t_count = 0
#initialize variable to hold the number of heads and tails
h_t = ['h', 't']
#define the sample space
while total < 100 :
#keeps track that tosses does not exceed 100
toss = random.sample(h_t, 1)
#variable to hold the outcome of each coin toss
if toss[0] == 'h':
#heck if toss is head
h_count+=1
Increase count of heads. owee
print(toss[0], end=' ')
#display the sample selected
else:
#if not head, then it's tail
t_count+=1
#increase count yv
print(toss[0], end=' ')
total+=1
#
print('head counts : ', h_count, 'percentage : ', round(h_count/100, 2),'%')
print('tail counts : ', t_count, 'percentage : ', round(t_count/100, 2), '%')
# display result.
A sample run of the program is attached
Learn more: https://brainly.com/question/18581972
We appreciate your time. Please come back anytime for the latest information and answers to your questions. We appreciate your visit. Our platform is always here to offer accurate and reliable answers. Return anytime. We're here to help at Westonci.ca. Keep visiting for the best answers to your questions.