Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Error in Program for Game of Craps

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in Program for Game of Craps

    Hello! I've been working on this programming assignment for a game of craps, and I can't seem to figure out how to correct an error. Here's my code:

    import java.util.*;
    import java.io.*;
     
    public class Craps
    {
         public static void main(String[] args)                         
        {                                
     
         Scanner scan = new Scanner(System.in);
     
         String YesNo;
         boolean Y = true;
     
         System.out.println("Hi there! Do you want to play some craps(Y/N)?");
         YesNo = scan.next();
     
         if (YesNo.equals("N"))
         System.exit(0);
         else if (YesNo.equals("Y"))
         {
          System.out.println("Do you want to read the rules of the game(Y/N)?");
          YesNo = scan.next();
          {
           if (YesNo.equals("N"))
           play();     
           else if (YesNo.equals("Y"))
           {
            rules();
            play();
           }
     
          } 
         }
         System.exit(0);
     }
     
     public static void rules()
     {
         System.out.print("Two die are thrown and the \n"
                        + "sum of their two faces are added up, giving a number from \n"  
                        + "2 to 12. If the number is 7 or 11, the thrower wins. If \n"
                        + "the number is either 2, 3, or 12, the thrower loses. If it is \n"
                        + "neither of these numbers (i.e., it is either 4, 5, 6, 8, \n"
                        + "9, or 10), this number becomes the point. The thrower \n"
                        + "continues to throw the dice until either his point comes \n"
                        + "up or the number 7 comes up. If the point comes up, the \n"
                        + "thrower wins. If a 7 comes up, the thrower loses. (One \n"
                        + "says he \"crapped out\"" + "). If the thrower wins, he plays \n"
                        + "again. If he loses, he gives up the dice to the next player.\n"
                        + "(Hence, the term \"crapped out\"" + " or lose the throw.)\n ");
     }
     
    public static boolean play()
    {
            Die dieOne = new Die();
            Die dieTwo = new Die();
            int result;
            int points;
            int rollcount = 0;
     
            result = rollDie(dieOne, dieTwo);
            System.out.println("The result of the two rolls are " + result);
     
                switch (result)
                {
                    case 7:
                    case 12: 
                        System.out.println("You won the game.!");
                        System.exit(0);
                        break;
                    case 3:
                    case 11:
                        System.out.println("You lost the game. ");
                        System.exit(0);
                        break;
                    default:
                        result = points;
                        result = rollDie(dieOne, dieTwo);
                        System.out.println("Your total points are " + points + result);
                    }
     
                    rollcount++;
    }        
     
     
     
    private static int rollDie(Die one, Die two)
    {
        Die dieOne = new Die();
        Die dieTwo = new Die();
        int result;
     
        one.roll();
        two.roll();
     
     
        result = dieOne + dieTwo;
    }
     
     
     
    public class Die
    {
        //The smallest number a die can have
        private final int MIN_NUMBER = 1;
        //The largest number a die can have
        private final int MAX_NUMBER = 6;
        //The initialization of the die when created
        private final int NO_NUMBER = 0;
     
        //the current value of the die
        int number;
     
        public Die()
        {
            number = NO_NUMBER;   
        }
     
        //this procedure rolls the die
        public void roll()
        {
             number = (int) Math.floor(Math.random()*(MAX_NUMBER - MIN_NUMBER + 1)) +1;
        }   
     
        public int getNumber()
        {
            return number;   
        }
     
    }
     
    }

    The error I get when I attempt to compile the code is "non-static variable this cannot be referenced from a static context" and it highlights "Die dieOne = new Die();"

    I'm terrible at Java so I apologize if there is a simple fix that I was too slow to figure out haha. Also, if there are any other major problems you see with my coding I'd greatly appreciate if you'd point them out to me (although I'm sure BlueJ will do that for me eventually lol).


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Error in Program for Game of Craps

    I would just remove the static modifier from all your methods. That should fix your error.

    However, there is a lot more wrong with your code. Consider your rollDie method. None of its
    local variables achieve anything! Once you realise that, you'll probably decide that the method
    itself is pointless, and find a better way to achieve what you want...

Similar Threads

  1. Game of Craps: won't compile
    By FreeBird in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 15th, 2010, 05:12 PM
  2. Craps- GAME fine tunning.
    By Beaney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 3rd, 2010, 08:32 PM
  3. Need help with program please: getting symbol error
    By blinkzz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 18th, 2009, 02:23 AM
  4. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  5. Error while creating Gym member database through Java programming
    By parvez07 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 26th, 2009, 02:17 AM