Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Coin Toss Program- Output issue

  1. #1
    qwerty5683 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Coin Toss Program- Output issue

    I am trying to create a program that asks you the number of times you want a coin to be tossed, and then outputs the number of times it "landed" on heads in that inputted number of times. I want it to output something like "number of heads:". Instead my program outputs multiple lines saying "number of heads: *a number*" or "number of tails: *a number*" Could someone help me create the output I want by modifying my program?


    Java Code:
    import java.util.Scanner;  
     public class Lab04  
     {  
       public static void main(String [] args)  
         {  
             Scanner in;  
             in = new Scanner(System.in);  
                
             System.out.println("Enter the number of flips: ");  
             int numberOfFlips = in.nextInt();  
             int numberOfHeads = 0;  
             int numberOfTails = numberOfFlips - numberOfHeads;
             System.out.println("\n");  
                
             double randomNumber = 0;  
             int counter = 0;             
              while(counter < numberOfFlips)  
             {  
                    
                 randomNumber = Math.random();  
                 counter ++;  
                    
                    
                 boolean isTails = randomNumber > .5;  
                    
                 if (isTails)  {  
                  System.out.println("\nNumber of Tails: " + numberOfTails);
                 }  
                 else {  
                     numberOfHeads++;  
                     System.out.println("\nNumber of Heads: " + numberOfHeads);
                      
                      
                 }  
                    
                    
             }  
               
              
                
         }  
    }

  2. #2
    sagarvk is offline Member
    Join Date
    Jan 2015
    Posts
    25
    Rep Power
    0

    Default Re: Coin Toss Program- Output issue

    Try this code.It gives output as you want.
    Java Code:
    import java.util.Scanner;  
     public class JavaApplication18  
     {  
       public static void main(String [] args)  
         {  
             Scanner in;  
             in = new Scanner(System.in);  
                 
             System.out.println("Enter the number of flips: ");  
             int numberOfFlips = in.nextInt();  
             System.out.println("\n");  
              
             double randomNumber = 0;  
             int counter = 0;       
             int heads=0,tails=0;
              while(counter <numberOfFlips)  
             {  
                     
                 randomNumber = Math.random();  
                 counter ++;  
                     
                     
                 boolean isTails = randomNumber > .5;  
                     
                 if (isTails)  {  
                    tails=tails+1;
                 }  
                 else {  
                      heads=heads+1;
                 }  
             }  
               System.out.println("\nNumber of Tails: " + tails); 
               System.out.println("\nNumber of Heads: " + heads);
                 
         }  
    }

  3. #3
    qwerty5683 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Coin Toss Program- Output issue

    Works perfectly! Thanks so much!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Coin Toss Program- Output issue

    @OP: you should think a bit before you start banging you keyboard: if you flip a coin N times there'll be n heads and N-n tails and the number n has a uniform distribution over the interval [0, N]; so a simulation could do:

    Java Code:
    Random r= new Random();
    int heads= r.nextInt(N+1);
    int tails= N-heads;
    You don't need any silly loops ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Coin Toss Program- Output issue

    @sagarvk: don't spoonfeed the OP with incorrect code (the test should be 'randomNumber >= 0.5'); if you do it a next time your code will be removed.
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by sagarvk View Post
    Try this code.It gives output as you want.
    Didn't we agree you should NOT be handing out code like this? This is clearly a learning assignment. The OP has learned nothing, but he will get a good grade in CS class. This means that the NEXT assignment will be even harder. And ofcourse, you're there to fix him up again. Now he has learned nothing again, but STILL he gets a good grade. The next assignment... You see where this is going?

    Next time, point out where the problem is, so the OP can fix them himself.
    Last edited by SurfMan; 02-02-2015 at 08:43 AM.
    DarrylBurke likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by SurfMan View Post
    [URL="http://www.java-forums.org/new-java/94096-help-creating-code.html#post402800"]The OP has learned nothing, but he will get a good grade in CS class.
    That of course depends also on the teacher. If I ever had tried to pull that on mine he would have caught me in two seconds flat with a simple question; 'so can you explain to me how this code works?'
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by gimbal2 View Post
    That of course depends also on the teacher. If I ever had tried to pull that on mine he would have caught me in two seconds flat with a simple question; 'so can you explain to me how this code works?'
    That reminds me of my Dutch teacher. I had to do an oral report about my written bookreport about "De Aanslag" by Mulisch. After the first three questions he could say not only that I copied the report, but also from whom. I never copied a bookreport since. Honestly. I didn't.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Coin Toss Program- Output issue

    Like that stupid French book report; I never read the original book, only the translation; I guessed the title: "Seul sur me monde"; it was "Remi". I dropped French ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by JosAH View Post
    Like that stupid French book report; I never read the original book, only the translation;
    Wow that never occurred to me. Read the translation. You learn something new every day - and that might make you feel a little more dumb.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by JosAH View Post
    @OP: you should think a bit before you start banging you keyboard: if you flip a coin N times there'll be n heads and N-n tails and the number n has a uniform distribution over the interval [0, N]; so a simulation could do:

    Java Code:
    Random r= new Random();
    int heads= r.nextInt(N+1);
    int tails= N-heads;
    You don't need any silly loops ...

    kind regards,

    Jos
    Well, in this case I can't refute the math for lack of knowledge. But the silly loops version gives the expected results consistently with a small error. Your version above gives results all over the place. So perhaps I am misunderstanding something.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by jim829 View Post
    Well, in this case I can't refute the math for lack of knowledge. But the silly loops version gives the expected results consistently with a small error. Your version above gives results all over the place. So perhaps I am misunderstanding something.
    What do you mean by "all over the place"? All my 'simulation' does is predict the number of heads/tails (just as the OPs assignment asked).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by JosAH View Post
    What do you mean by "all over the place"? All my 'simulation' does is predict the number of heads/tails (just as the OPs assignment asked).
    Ah, just a prediction. Not to simulate the flipping of a coin.

    I'm off to a bad start this week. I would like to rewind to Saturday.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    sagarvk is offline Member
    Join Date
    Jan 2015
    Posts
    25
    Rep Power
    0

    Default Re: Coin Toss Program- Output issue

    I am sorry once again guys.I wont do it again.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Coin Toss Program- Output issue

    Quote Originally Posted by jim829 View Post
    Ah, just a prediction. Not to simulate the flipping of a coin.

    I'm off to a bad start this week. I would like to rewind to Saturday.
    I'd like to go back to last Friday; the sum of N uniform distributed variables [0, 1] is N/2, and the standard deviation is N/12, so the Random.nextint( ... ) method just doesn't cut it; we need the nextGaussian( ... ) method here and transform the result to a normal distribution with mean N/2 and standard deviation of N/12; I'm such an oaf ... (see the Irwin/Hall results (google is your friend)).

    *blush*

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    qwerty5683 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Coin Toss Program- Output issue

    Since I am new to programming, showing me the correct code is far more helpful then simply explaining what to do. I am a visual learner. If I had asked my professor for help, he wouldn't have helped as much. Also, this program is an in class example, not a programming project or assignment. I appreciate everyone's input though.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Coin Toss Program- Output issue

    Well, people only learn by their failures, not by their successes. So if someone does it for you they have removed the opportunity for you to make a mistake and thus to learn. I believe this is true whether you are new or experienced at programming.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    qwerty5683 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Coin Toss Program- Output issue

    I have failed, multiple times, with creating this program. That's why I posted my issue on this forum. If I had figured out what was wrong from my failures, I wouldn't have needed to post it. Therefore, no one took away the opportunity for me to fail.

Similar Threads

  1. problem with enum in my coin toss program.
    By Azaera in forum New To Java
    Replies: 5
    Last Post: 06-21-2012, 05:09 AM
  2. Replies: 7
    Last Post: 10-31-2011, 01:05 AM
  3. help with my coin toss program please?
    By tran21 in forum New To Java
    Replies: 6
    Last Post: 10-30-2011, 05:20 AM
  4. Trying to finish coin toss project
    By learn in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 11:54 PM
  5. coin toss program(Eclipse)
    By ccie007 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 07:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •