CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Talking Chance Game Java: Need Help Getting it To Run

    I need help, it won't run. It only prints my message I put in main. What's wrong? Please help.

    It compiles fine, and it just gives me an exception in thread "main" error.

    Code:
    import java.util.Random;
    
    public class Dice{
    
    
    //create random number generator for roling the dice.
            private Random random = new Random();
    
    //enumeration with constants that stand for the game status
            private enum Status{CONTINUE, WON, LOST};
    
    //simulates a game of craps
    public void play(){
    
            Status gameStatus; //Can contain CONTINUE, WON, or LOST
            int myPoint = 0; //Point if user doesn't win or lose on first try.
    
            //srand((unsigned)time(NULL));
            int sum=TwoDice();
    
            // Cases determined on first roll.
            switch(sum)
            {
                    case 7:
                    case 11:
                            gameStatus=Status.WON;
                            break;
    
                    case 2:
                    case 3:
                    case 12:
                            gameStatus=Status.LOST;
                            break;
    
                    default:  //for cases if there's no win or lose yet.
                            gameStatus=Status.CONTINUE;  //game continues.
                            myPoint=sum;            //remembers the point
            } //end of switch 
    
            //while game is not finished yet. 
            while(gameStatus==Status.CONTINUE){
                    sum=TwoDice(); //Rolls dice again.
                    
                    //Finds out game status once more.
                            if(sum == 7)
                                    gameStatus=Status.LOST;
                            else
                                    if(sum == myPoint)
                                            gameStatus=Status.WON;
                    
            }
            if (gameStatus==Status.WON)
                    System.out.println("Player wins");
            else
                    System.out.println("Player loses");
    } //end of play
            //Sum of the dice.
            public int TwoDice(){
                    //Get random dice values.  
                    int die1 = 1+random.nextInt(6);  //first die roll
                    int die2 = 1+random.nextInt(6);  //second die roll
                    int sum = die1+die2;  //sum of die values
                            
                    //Prints the results of this roll.
                    System.out.println("Player rolled: "+die1 +die2);
                    System.out.println("Sum is: "  +sum);
                    return sum;
            }//end of TwoDice
             
            public static void main(String[]args){
                    System.out.println("Craps Simulator");
            }   
    } //end of Dice
    Last edited by coder752; July 24th, 2009 at 12:59 PM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured