1. Member
Join Date
Feb 2013
Posts
7
Rep Power
0

## Craps score

I'm having trouble with keeping the scores. Whenever I try playing again, the current score doesn't add up to the previous one.

Java Code:
```import java.util.Scanner;
public class Craps {
public static void main(String[] args) {

int die;
die = 1;
Scanner input = new Scanner(System.in);

while(die != 0){

System.out.print("Enter 1 to play or 0 to exit: ");

die = input.nextInt();

switch (die){

case 1:
int dice1 = (int)(Math.random()*6) + 1;
int dice2 = (int)(Math.random()*6) + 1;
Dice(dice1, dice2);
break;

case 0:
System.out.println("Game over.");
System.exit(0);
break;
}
}
}

public static void Dice(int dice1, int dice2){

int point = 0;

int total = dice1 + dice2;

point += total;

if (total == 2 || total == 3 || total == 12) {
System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
System.out.println("You lose!");
System.out.println();
}
else if (total == 7 || total == 11) {
System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
System.out.println("Natural. You win!");
System.out.println();
}
else if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10) {
System.out.println("Point: " + total);
System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + total);
System.out.println("You win!");
System.out.println();
}

}
}```
Last edited by lynkxs; 02-26-2013 at 09:59 AM.

2. ## Re: Craps

First, put your code in tags to keep formatting and make it readable.

Second, your point variable in method Dice (methods should actually start with a lower case letter) is reset to 0 every time the method is called. Move the variable to the class level, i.e:
Java Code:
```class Craps {
private static int point;
...
}```
You can now access the variable from method Dice and it won't be reset every time.

3. Member
Join Date
Feb 2013
Posts
7
Rep Power
0

## Re: Craps score

I see, thank you for your help.

#### Posting Permissions

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