I have to create a program that simulates flipping a coin. The directions my teacher gave me are as follows:

1. Program should prompt the user for number of runs
2. Program should prompt the user for the number of times to flip the coin in a single run
3. Limit the number of flips per run from 1 - 100,000
4. Limit the number of flips from 2 - 20
5. Program should display "H" for heads and "T" for tails for each individual toss off the coin
6. Program should display each run as a series of tosses
7. Program should display the total number of runs that had all HEADS and the total number of runs that had all TAILS
8. Program should display a message indicating a <<<ALL HEADS>>> or <<<ALL TAILS>> run

The example output should be like this (6 runs with 3 flips per run)

H T H
H T T
H H H <<ALL HEADS>>>
T T H
T H T
T T T <<ALL TAILS>>>

Number of runs with all heads: 1
Number of runs with all tails: 1

I'm about as far as the inputs and I know the math to find heads or tails, but doing everything else output wise has me stuck. I'm not expecting to be spoon fed the entire code, but I really would like some help.

First thing you need is a list of all the steps the program needs to do to solve the problem. So make a list of the steps.
When you have them, then take them one at a time and figure out how to write the code in java to do them.

Please ask some specific questions.

import java.util.Scanner;


public class CoinFlipping
	{
		public static void main(String[] args)
			{
				int r=0; // number of runs (r)
				int f=0; // number of flips per run (f)
				
				Scanner input = new Scanner(System.in);
				
				
				System.out.println("Please enter the number of runs (1 - 100,00): ");
				r = input.nextInt();
				
				System.out.println("Please enter the number of flips per run (2 - 20): ");
				f = input.nextInt();


				while(r<=100000 && f<=20)
					if(Math.random() < 0.5)
						System.out.println("H");
					else
						System.out.println("T");
					
				
			}
	}

I'm not sure if this will help, but this is what I have so far. However, the output just infinite loops HTHTHTHTHTHTHTHTHTHTHTHTHTHTH so I KNOW that I'm getting somewhere, but it's incorrect.

for testing I'd leave out the huge number. Work with something like 20 until you get the logic worked out.

Your code prompts for two numbers and then what do you want it to do?
Make the list of steps that the program is supposed to do next BEFORE writing the code.

When will the while loop stop looping? Where inside the loop do you change the values of r or f?

Member Avatar for ztini

Your when loop is the wrong approach. You are flipping n of times for m number of runs. Any time you are looping for a constant that should throw a flag up that you should be using a for loop. Or, in your case a nested for loop. Like this:

import java.util.Scanner;


public class CoinFlip {
	
	public static void go() {
		Scanner in = new Scanner(System.in);
		
		System.out.print("Runs: ");
		int totalRuns = in.nextInt();
		
		System.out.print("Flips/Run: ");
		int totalFlips = in.nextInt();
		
		for (int run = 0; run < totalRuns; run++) {
			for (int flip = 0; flip < totalFlips; flip++)
				System.out.println(Math.random() < 0.5 ? "H" : "T");
		}
	}

	public static void main(String[] args) { 
		CoinFlip.go();
	}	
}

Output:

Runs: 5
Flips/Run: 2
H
T
H
T
T
H
T
H
H
H

Thank you so much! I knew the problem was somewhere in my for loop and declarations!
You're a life saver! :)

Yes, copying and pasting is a lot easier way to get assignments done.

No no, before I had gotten this answer I had started to set up a for loop after thinking it over and even writing it down.

Then it hit me that I needed an accumulator, I know that's kind of more VB speak, but that's what totalRuns and totalFlips represent, or in my case r and f.

Don't assume I'd just cheat and copy paste my homework, when I don't get it I brainstorm a lot until I do.

End Product:

import java.util.Scanner;

public class HeadTailCounter
	{
						
			public void flip()
				{
			
					Scanner input = new Scanner(System.in);
				
					System.out.println("Enter the number of runs (1 - 100,000): ");
					int r = input.nextInt();
				
					System.out.println("Enter the number of flips per run (1 - 20): ");
					int f = input.nextInt();
					
					if(r<=100000 && f<=20)
						{
							for (int run = 0; run < r; run++) 
								{
									for (int flip = 0; flip < f; flip++)
										System.out.print(Math.random() < 0.5 ? "H " : "T ");
										System.out.println();
								}
						}
					else
						System.out.println("Please check your entry and try again.");
						
				}
				
		public static void main(String[] args)
			{
				HeadTailCounter coin = new HeadTailCounter();
				
				coin.flip();
			}

	}

this isn't printing out "<<<all tails>>>" nor "<<<all heads>>>" and not keeping track of the number of times both these events happen.

No it's not, but my main issue has been solved concerning the runs and flips.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.