Question

java beginner level NO ARRAYS in program Flip a coin (Assignment) How many times in a...

java

beginner level

NO ARRAYS in program

Flip a coin (Assignment)

How many times in a row can you flip a coin and gets heads?

Use the random number generator to simulate the flipping of the coin. 0 means heads, 1 means tails.

Start a loop,

flip it, if heads, count it and keep flipping.

If tails, stop the loop.

Display the number of times in a row that heads came up.

Once this is working, wrap a while loop around the loop that flips the coin to play again if the user says they want to.

Run and test this loop.

Sample Output: You flipped the coin and got HEADS 0 times in a row!

Want to try again? (Y or N) Y

You flipped the coin and got HEADS 3 times in a row!

Want to try again? (Y or N) N

Thank you for playing. Have a good day.

Homework Answers

Answer #1

Code

import java.util.Scanner;
public class Flip {

  public static void main(String[] args) 
  {
     Scanner in = new Scanner(System.in);
     while(true) //loop till enters N
     {
       int c=0;
       while(true) //loop till tail flipped
       {
         int r=(int)(Math.random() * 2);
         if(r==0)
         {
           c++;
         }
         else
         {
           break;
         }
       }
       System.out.println("You flipped the coin and got HEADS "+c+" times in a row!");
       System.out.print("Want to try again? (Y or N) ");
       char ch=in.next().charAt(0);
       if(ch=='N')
       {
         System.out.println("Thank you for playing. Have a good day.");
         break;
       }
     }
  }

}

Terminal Work

.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are flipping a fair coin with one side heads, and the other tails. You flip...
You are flipping a fair coin with one side heads, and the other tails. You flip it 30 times. a) What probability distribution would the above most closely resemble? b) If 8 out of 30 flips were heads, what is the probability of the next flip coming up heads? c) What is the probability that out of 30 flips, not more than 15 come up heads? d) What is the probability that at least 15 out 30 flips are heads?...
You flip a coin until getting heads. Let X be the number of coin flips. a....
You flip a coin until getting heads. Let X be the number of coin flips. a. What is the probability that you flip the coin at least 8 times? b. What is the probability that you flip the coin at least 8 times given that the first, third, and fifth flips were all tails? c. You flip three coins. Let X be the total number of heads. You then roll X standard dice. Let Y be the sum of those...
If you flip a fair coin and get heads 10 times in a row, what is...
If you flip a fair coin and get heads 10 times in a row, what is the chance of getting tails on the next flip? Explain.
Flip a coin 100 times and record the outcome (how many times heads, how many times...
Flip a coin 100 times and record the outcome (how many times heads, how many times tails), then create a table in Microsoft word and record your data.
If you flip a coin 56 times, how many times would you expect to get either...
If you flip a coin 56 times, how many times would you expect to get either 3 heads or 3 tails?
Question 3: You are given a fair coin. You flip this coin twice; the two flips...
Question 3: You are given a fair coin. You flip this coin twice; the two flips are independent. For each heads, you win 3 dollars, whereas for each tails, you lose 2 dollars. Consider the random variable X = the amount of money that you win. – Use the definition of expected value to determine E(X). – Use the linearity of expectation to determineE(X). You flip this coin 99 times; these flips are mutually independent. For each heads, you win...
You flip a fair coin. If the coin lands heads, you roll a fair six-sided die...
You flip a fair coin. If the coin lands heads, you roll a fair six-sided die 100 times. If the coin lands tails, you roll the die 101 times. Let X be 1 if the coin lands heads and 0 if the coin lands tails. Let Y be the total number of times that you roll a 6. Find P (X=1|Y =15) /P (X=0|Y =15) .
) Flip a coin 100 times. Please Show your work below. a) How many times did...
) Flip a coin 100 times. Please Show your work below. a) How many times did Heads Appear? b) What was the longest streak of Heads or Tails?
If I tell you that I have a coin, but it is a special coin. In...
If I tell you that I have a coin, but it is a special coin. In that, I mean that it is not a 50/50 coin. However, I don't know how likely it is for it to land on heads (or tails). So we decide to flip it 10 times and we get 40% heads (which is 0.40 as a proportion). Are you willing to say that it is a 40/60 coin? Why or why not? after we flipped it...
Suppose you flip a fair coin six times where A is the number of tails and...
Suppose you flip a fair coin six times where A is the number of tails and B is the number of heads. Prove if E(X*Y) is not equal to E(X)E(Y).
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT