8 Replies - 1283 Views - Last Post: 10 March 2013 - 08:57 PM Rate Topic: -----

#1 Rawdogz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 Heads = 0;
  int Tails = 0;
 
   
 int tries = 0;
 int result = rand.nextInt(0+1);
    
while(H != 3 || T !=3)  
   
   if (result == H){
     H = H + 1;
    System.out.println("Head"); 
     Heads++;
   }
  
   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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,532
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Rawdogz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :(
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,532
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Coin Flip help

Posted 09 March 2013 - 02:52 PM

View PostRawdogz, 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 ?
Was This Post Helpful? 0
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 329
  • Joined: 29-September 12

Re: Coin Flip help

Posted 09 March 2013 - 03:03 PM

View PostRawdogz, 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)

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,297
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 Rawdogz  Icon User is offline

  • New D.I.C Head

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

Re: Coin Flip help

Posted 10 March 2013 - 08:56 AM

View Postpbl, on 09 March 2013 - 02:52 PM, said:

View PostRawdogz, 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.
Was This Post Helpful? 0
  • +
  • -

#9 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 329
  • Joined: 29-September 12

Re: Coin Flip help

Posted 10 March 2013 - 08:57 PM

View PostRawdogz, on 10 March 2013 - 08:56 AM, said:

View Postpbl, on 09 March 2013 - 02:52 PM, said:

View PostRawdogz, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1