Results 1 to 2 of 2
  1. #1
    cmballer is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Random Number Game CSI help

    I have a program for my Computer Science I class that I need to do, and the description is:

    A game between players A and B is played as followed.
    A coin is tossed three times or until a head comes up, whichever occurs first.
    As soon as a head comes up, player A collects $1 from player B.
    If no head comes up on any of the three tosses, player B collects $6 from player A.
    In either case, the game is over.
    Your program is to simulate this game 1000 times to help decide whether A or B has the advantage, or if it's a fair game.
    Assume that A and B both start with $100 in each game.

    When I run the code that I have, it doesn't print anything except for the first System.out.print before the for loop, so I was wondering if you could help me figure out if there are any errors I have in my code.

    Java Code:
    public class BonusProgram3{
        public static void main(String args[])
        { 
           
           int i,zha,ZhangA=100,ZhangB=100;
           
           System.out.println("Player A and Player B both start with $100. \nAfter simulating the game 100 times, here are the results.");
           
           for(i=1; i<=1000; i++){
               
            int coin[]=new int[3];
                      
                for(zha=0; zha<3; i++){
                    coin[zha]=(int)(Math.random()*2);
                    if(coin[zha]==1){
                        ZhangA=ZhangA+1;
                        ZhangB=ZhangB-1;
                    }
            }
            
            if(coin[0]==2 && coin[1]==2 && coin[2]==2){
                ZhangA=ZhangA-6;
                ZhangB=ZhangB+6;
            }
        }
        
        if(ZhangA>ZhangB)
            System.out.println("Player A, with $" +ZhangA+ " beats Player B, with $" +ZhangB+ ".");
            
        if(ZhangB>ZhangA)
            System.out.println("Player B, with $" +ZhangB+ " beats Player A, with $" +ZhangA+ ".");
            
        if(ZhangA==ZhangB)
            System.out.println("Player A, with $" +ZhangA+ " tied Player B, with $" +ZhangB+ ".");
        }
    }

    Thank you!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Random Number Game CSI help

    Add some more printlns to show the execution flow and the values of the variables as they change.

    Change the number of loops down from 1000 for testing with the printlns showing the execution.

Similar Threads

  1. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 07:47 PM
  2. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 10:13 AM
  3. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 03:20 PM
  4. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 02:42 PM
  5. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM

Posting Permissions

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