Computer Science Canada Dice game |
Author: | bishop [ Mon Apr 20, 2009 6:38 pm ] | ||||
Post subject: | Dice game | ||||
Hello there! I have to create a dice game where there are three dice. The user enters the number of times the three dice are rolled and what number he/she wants to bet on. Here's what I have
For some reason when it's about to draw each die,ie, when it reaches the case statement in the drawDie in the Dice class it says "Variable has no value" Right now I'm not concerned about the score just rolling and displaying the dice Any help would be appreciated. Thanks. Mod Edit: Remember to use syntax tags! Thanks
|
Author: | Dusk Eagle [ Mon Apr 20, 2009 6:53 pm ] |
Post subject: | Re: Dice game |
When submitting code, please use [syntax="turing"]%code here[/syntax]. This makes it much easier to read. The first thing I notice is that you forgot to define the class variable "value". Once I did that, I got an error at the line "new DrawDice" : "'DrawDice has not been declared." So, you're getting more errors with your code than the one you mentioned. But anyway, the problem you are having is that your variable "number" is not equal to anything. You must call your setValue procedure to assign a value to number before you can call your draw procedure. |
Author: | bishop [ Mon Apr 20, 2009 7:33 pm ] | ||
Post subject: | Re: Dice game | ||
Sorry about that. I made changes and forgot to do it throught the code Anyway now it should show the error the i mentioned earlier. It shows an error message "Variable has no value" when it gets to the drawDice in class Dice
Mod Edit: Your code goes where it says code here |
Author: | Dusk Eagle [ Mon Apr 20, 2009 8:02 pm ] | ||||
Post subject: | Re: Dice game | ||||
Here's your problem. Your object RollDice has a value in it, named number, that has been initialized through calling the rollDice procedure. However, an entirely different object, displayDice, does not have value "number" initialized to anything. Think of it this way:
The problem above is that Jill's age is completely unknown; we have only set Bob's age. This is the same thing you have done in your program. You must use the same instance of an object throughout your code, as RollDice's values will not equal displayDice's values, just as Bob's age will not equal Jill's age. |
Author: | bishop [ Tue Apr 21, 2009 4:49 pm ] |
Post subject: | RE:Dice game |
lol I thought I was still in the Game class while typing the main. You were absolutely right, it works now. Thank you so much for your help!! |