Results 1 to 4 of 4

# Thread: CoinToss help

- 03-10-2013, 09:48 PM #1Member
- Join Date
- Mar 2013
- Posts
- 11

- Rep Power
- 0

## CoinToss help

hi all, i am coding a coin flipping program that i am having a little trouble with. the program has to toss the coin until it has 3 of the same as well as return the number of try's it took to get it. this is what i came up with so far

import java.util.*;

public class CoinFlip2 {

public static void main(String[] args) {

Random rand = new Random();

int H = 1;

int T = 0;

int tries = 0;

int result = rand.nextInt(1) + 0;

while(H != 3 || T !=3)

if (result == H){

H = H + 1;

System.out.println("Head");

tries++;

}

else {

if (result == T){

T = T + 1;

System.out.println("Tail");

tries++;

}

}

System.out.println(tries);

}

}

i came across the

num1 = new random

num2 = new random

num3 = new random

while(num1 num2 and num3 are not equal) {

num1 = new random

num2 = new random

num3 = new random

println num1 and num2 and num3

}

i thought i understood what they was saying, but not so much.

thank you for any help in the right directions on this

- 03-10-2013, 10:29 PM #2Senior Member
- Join Date
- Oct 2010
- Posts
- 317

- Rep Power
- 4

## Re: CoinToss help

Hi Rawdogz, welcome to the forums.

I dont understand the comments below the code but you seem to be confusing counters with the values used in the conditions.

If for example the first random number is one which matches 'H'. 'H' is then increased by one to two which will never be generated by 'rand' and so the code will never be reached.

Instead of comparing 'result' to 'H' / 'T', you can call rand.nextInt(1) directly. The second if statement is redundent as if the result is not a head then its tails.

Your while condition is causing issues. It continues to loop until both 'H' and 'T' equal three which is unlikely.

It would also be advisable to follow naming conventions for variable names. All variables should begin with a lower case letter unless its a constant in which case all letters are usually upper case.

And please enclose you code within [code] tags.

Regards.

- 03-11-2013, 12:46 AM #3Member
- Join Date
- Mar 2013
- Posts
- 11

- Rep Power
- 0

## Re: CoinToss help

thank you for the reply Ronin attracted is the actual assignment.

Write a complete program that plays a coin flipping game, displays the individual flips, reports when a game is LOST or WON and shows the number of flips needed to complete the game.

The algorithm is as follows:

Simulate the flip of a coin using a JAVA random number generator.

Flip the coin once to initialize the flip value and print out the flip

Inside of a loop, repeatedly flip the coin until 3 consecutive flips have the same value (3 heads or 3 tails)

a. Display the flip results after each flip

When the game ends, report the total number of flips

Note: you can use 0 for heads and 1 for tails, but if you have time, add another method that will convert the integers 0 and 1 to the characters H and T

in my research on how to write this program i came across this model on how to write the program but i really didn't understand it

num1 = new random

num2 = new random

num3 = new random

while(num1 num2 and num3 are not equal) {

num1 = new random

num2 = new random

num3 = new random

println num1 and num2 and num3

}

- 03-11-2013, 11:24 AM #4Senior Member
- Join Date
- Oct 2010
- Posts
- 317

- Rep Power
- 4

## Re: CoinToss help

Ah, I understand where you were going with the code. 'H' and 'T' are character values and not the name of the variables to use.

I see what the snippet of code in #3 is doing but this doesn't apply directly to your problem. The problem with that code is it generates three random numbers and loops if these are not identical. An example would help explain the problem:

First go generates 0, 0, 1 which causes the code to loop. Second go generates 1, 1, 0 which causes the code to loop. Based on your requirements though the program should have terminated as there were three consecutive 1s.

Forgetting the 'H' and 'T', in additon to the counter for the number of tries, you could use a simple counter to count the number of instances of one side (heads or tails). If the side changes then the counter is reset and it starts again. You could use a boolean value to determine if you are currently counting heads or tails.

Once the counter reaches three then print out the number of tries required.

Regards.

## Bookmarks