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

1. ## dice game problem

Hello guys! I am a beginner in Java with a little experience in programming and from the first time i loved java so i am trying to improve my shelf reading some books firstly and maybe later make some bigger projects with help from tutorials! But now i am just reading the book Absolute Java and i am stacked at a example so i would like some advice...by the way i am using only cmd for compile since i think that i should move to netbean a bit more later!!!
The program that i made it is about the Game of craps that it dice game...

Specificly "
In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are
rolled; the first roll of the dice in a craps round is called the “come out roll.”
A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12
automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number
becomes “the point.” The player keeps rolling the dice until either 7 or the point is
rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first,
then the player loses.
Write a program that simulates a game of craps using these rules without human
input. Instead of asking for a wager, the program should calculate whether the
player would win or lose. The program should simulate rolling the two dice and
calculate the sum. Add a loop so that the program plays 10,000 games. Add
c ounters that count how many times the player wins, and how many times the
player loses. At the end of the 10,000 games, compute the probability of winning
[i.e., Wins / (Wins + Losses)] and output this value. Over the long run, who
is going to win the most games, you or the house?"

I am half done it expect the part of calculating the wins/looses and the loop of 10 000 game that it is unnecessary!
I am trying to "play" with the dices! So there is my code

```import java.util.Scanner;
import java.util.Random;

public class GameCraps {

public static void main(String[] args) {
Random generator=new Random();

Scanner keyboard=new Scanner(System.in);
System.out.println("**Game of Craps**");
int r=keyboard.nextInt();
do{
int dice1=generator.nextInt(6);
int dice2=generator.nextInt(6);
} while(r==1);

int sumdices=dice1 + dice2;

if(sumdices==7 || sumdices==11) {
System.out.println(sumdices);
System.out.println("We have a winner's come out roll!");
System.out.println("Player wins!");
System.exit(0);

}

if(sumdices==2 || sumdices==3 || sumdices==12 ) {
System.out.println(sumdices);
System.out.println("We have a looser's come out roll!");
System.out.println("Player looses!");
System.exit(0);

}

if(sumdices==4 || sumdices==5 || sumdices==6 || sumdices==8 || sumdices==9 || sumdices==10) {
System.out.printf("%d",sumdices);
System.out.println("A point is setted!");
int point=sumdices;
}

r=keyboard.nextInt();
do {
int dice1=generator.nextInt(6);
int dice2=generator.nextInt(6);
int newsumedices=dice1+dice2;

} while(r==1);

System.out.println(newsumedices);

if(newsumedices==point) {
System.out.println("You catched the point!You win");
System.exit(0);
}
else if (newsumedices==7) {
System.exit(0);

}

}

}```

When i am compiling via javac on cmd throws 6 erros of the type :

GameCraps.java:17: error: cannot find symbol
int sumdices=dice1 + dice2;
^
symbol: variable dice1
location: class GameCraps
GameCraps.java:17: error: cannot find symbol
int sumdices=dice1 + dice2;
^
symbol: variable dice2
location: class GameCraps
GameCraps.java:51: error: cannot find symbol
System.out.println(newsumedices);
^
symbol: variable newsumedices
location: class GameCraps
GameCraps.java:53: error: cannot find symbol
if(newsumedices==point) {
^
symbol: variable newsumedices
location: class GameCraps
GameCraps.java:53: error: cannot find symbol
if(newsumedices==point) {
^
symbol: variable point
location: class GameCraps
GameCraps.java:57: error: cannot find symbol
else if (newsumedices==7) {
^
symbol: variable newsumedices
location: class GameCraps
6 errors

2. ## Re: dice game problem

Several of the cannot find symbol errors are because the named variable's definition is not in scope where you are trying to use it. Move the variable's definition so that it is within the same pair of {}s with where you are trying to reference it.
You can define a variable in one place and assign it values in another.

[code=java]
[/code]
to get highlighting and preserve formatting.

3. ## The Following User Says Thank You to Norm For This Useful Post:

Nick87 (March 23rd, 2013)

4. ## Re: dice game problem

wow amazing the parameter =java to the code tag!!! surely helps alot the readers! Graaats!!!
But what you mean about the variables definition?
Should i define the variables that i am using inside the "ifs" or the loops?
Or you mean define my variables in the start of my class and then giving values?

5. ## Re: dice game problem

Define the variables at the levels within the {}s where they are going to be used.
If different methods need to access them, define them at the class level.
If they are only used in a method, define them in the method outside of any loops of if statements.
If they are only used locally with the current pair of {}s define them there.
```{
int x = 2; //  define x inside these {}s
//  x is known here
}
// x is NOT known outside the {}s```

Read up on the scope of variables.

Should i define the variables that i am using inside the "ifs" or the loops?
Only if you are not going to use them outside of the enclosing {}s

6. ## The Following User Says Thank You to Norm For This Useful Post:

Nick87 (March 23rd, 2013)

7. ## Re: dice game problem

oh thank you so much! how stupid of me to make my variables only variables of a loop! so basic of java! thank you so much anyway!
My new code but still i have a error message

GameCraps.java:57: error: variable point might not have been initialized
if(newsumedices==point) {
^
1 error

but point and newsumedices are definied!

EDID:I fixed by moving point=sumdices; out of } blocks of If...so now my program compiles but act so crazy! firstly when i am pressing 1 for the rolling it does nothin! secondly when i am pressing any other number throws me some crazy numbers

```import java.util.Scanner;
import java.util.Random;

public class GameCraps {

public static void main(String[] args) {
Random generator=new Random();

Scanner keyboard=new Scanner(System.in);
System.out.println("**Game of Craps**");
System.out.println("**Press number 1 to roll");
int r=keyboard.nextInt();
int dice1;
int dice2;
int point;
int newsumedices;

do{
dice1=generator.nextInt(6);
dice2=generator.nextInt(6);
} while(r==1);

int sumdices=dice1 + dice2;

if(sumdices==7 || sumdices==11) {
System.out.println(sumdices);
System.out.println("We have a winner's come out roll!");
System.out.println("Player wins!");
System.exit(0);

}

if(sumdices==2 || sumdices==3 || sumdices==12 ) {
System.out.println(sumdices);
System.out.println("We have a looser's come out roll!");
System.out.println("Player looses!");
System.exit(0);

}

if(sumdices==4 || sumdices==5 || sumdices==6 || sumdices==8 || sumdices==9 ) {
System.out.printf("%d",sumdices);
System.out.println("A point is setted!");

}
point=sumdices;

r=keyboard.nextInt();
do {
dice1=generator.nextInt(6);
dice2=generator.nextInt(6);
newsumedices=dice1+dice2;

} 	while(r==1);

System.out.println(newsumedices);

if(newsumedices== point) {
System.out.println("You catched the point!You win");
System.exit(0);
}
else if (newsumedices==7) {
System.exit(0);

}

}

}```