# Error in Program for Game of Craps

• February 21st, 2010, 10:36 PM
TheAsianMenace
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:

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).
• February 23rd, 2010, 04:31 AM
Shambolic
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...