# Coin Flip help

Page 1 of 1

## 8 Replies - 4652 Views - Last Post: 10 March 2013 - 08:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314883&amp;s=9478c2b25d60b4273395c1a68d270853&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Rawdogz

Reputation: 0
• Posts: 4
• Joined: 09-March 13

# Coin Flip help

Posted 09 March 2013 - 01:56 PM

hi everyone
trying to see if i could get a little help with this program i am writing

"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’
"

This is what i got so far, but its not printing the right answer.
```import java.util.*;
public class CoinFlip {
public static void main(String[] args) {
Random rand = new Random();
int H = 1;
int T = 0;

int Tails = 0;

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

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

if (result == H){
H = H + 1;
}

else {
if (result == T && T <= 3){
T = T + 1;
System.out.println("Tail");
Tails++;
}
}

System.out.println(tries);

}
}

```

thanks for any help

This post has been edited by g00se: 09 March 2013 - 02:01 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0

## Replies To: Coin Flip help

### #2 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Coin Flip help

Posted 09 March 2013 - 02:02 PM

A do while loop

do

generate 3 random numbers.

println num1 num2 and num3.

while num1 and num2 and num3 are not equal.

### #3 Rawdogz

Reputation: 0
• Posts: 4
• Joined: 09-March 13

## Re: Coin Flip help

Posted 09 March 2013 - 02:20 PM

we have not learned "do while", just "while". so i cant use it

### #4 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: Coin Flip help

Posted 09 March 2013 - 02:44 PM

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
}

This post has been edited by farrell2k: 09 March 2013 - 02:45 PM

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Coin Flip help

Posted 09 March 2013 - 02:52 PM

Rawdogz, on 09 March 2013 - 04:20 PM, said:

we have not learned "do while", just "while". so i cant use it />

So if you had learned C or C++ you would have to forget all the concepts learned while coding with these other languages ? Is it a prerequesite for your course that Java is the first language your learn ?

### #6 RozenKristal

• D.I.C Regular

Reputation: 45
• Posts: 381
• Joined: 29-September 12

## Re: Coin Flip help

Posted 09 March 2013 - 03:03 PM

Rawdogz, on 09 March 2013 - 02:20 PM, said:

we have not learned "do while", just "while". so i cant use it />

they essentially the same, except that do first, then enter the conditional part, so:

```while(whateveristrue)
{
body
}

do
{
body
}while(whateveristrue)
```

### #7 g00se

• D.I.C Lover

Reputation: 3542
• Posts: 16,146
• Joined: 20-September 08

## Re: Coin Flip help

Posted 09 March 2013 - 03:39 PM

Quote

```int result = rand.nextInt(0+1);
```
Will give you a random number between zero and ... zero

You need to clear accumulated heads if tails is thrown and vice versa

This post has been edited by g00se: 09 March 2013 - 03:39 PM

### #8 Rawdogz

Reputation: 0
• Posts: 4
• Joined: 09-March 13

## Re: Coin Flip help

Posted 10 March 2013 - 08:56 AM

pbl, on 09 March 2013 - 02:52 PM, said:

Rawdogz, on 09 March 2013 - 04:20 PM, said:

we have not learned "do while", just "while". so i cant use it />/>

So if you had learned C or C++ you would have to forget all the concepts learned while coding with these other languages ? Is it a prerequesite for your course that Java is the first language your learn ?

funny thing is i took C++ and that's just what i had to do. and if you use coding that he hasn't gone over yet, you lose points on the HW. it part of my Major now.

### #9 RozenKristal

• D.I.C Regular

Reputation: 45
• Posts: 381
• Joined: 29-September 12

## Re: Coin Flip help

Posted 10 March 2013 - 08:57 PM

Rawdogz, on 10 March 2013 - 08:56 AM, said:

pbl, on 09 March 2013 - 02:52 PM, said:

Rawdogz, on 09 March 2013 - 04:20 PM, said:

we have not learned "do while", just "while". so i cant use it />/>/>

So if you had learned C or C++ you would have to forget all the concepts learned while coding with these other languages ? Is it a prerequesite for your course that Java is the first language your learn ?

funny thing is i took C++ and that's just what i had to do. and if you use coding that he hasn't gone over yet, you lose points on the HW. it part of my Major now.

Ask the professor and see if you can use it, say they are the same thing, just that the conditional statement is evaluated later before the body.