# Thread: setting counter in craps game....

1. Member Join Date
Jan 2013
Posts
3
Rep Power
0

## setting counter in craps game....

I am trying to write a craps game wit no human input that counts the # of wins and losses and displays the probability of winning. I can not figure out why it is not counting right. Any help would be appreciated. Thanks

public class Craps
{

public static void main(String[] args)
{
int roll1 = (int)((Math.random()*6)+ 1);
int roll2 = (int)((Math.random()*6)+ 1);
int sum = roll1 + roll2;
int point = sum;
int win = 0;
int loss = 0;

for (int i = 0; i<=99; i++)
if ((sum == 7) || (sum == 11))
{
win++;
}
else if ((sum == 2)||(sum == 3)||(sum == 12))
{
loss++;
}
while ((sum != 7)&&(sum != point))
{
roll1 = (int)((Math.random()*6)+ 1);
roll2 = (int)((Math.random()*6)+ 1);
sum = roll1 + roll2;
}

if (sum == point)
{
win++;
}
else
{
loss++;
}
int probability = win/(win + loss);
System.out.println("The probability of winning is: " + probability);
}

}  Reply With Quote

2. ## Re: setting counter in craps game....  Reply With Quote

3. Member Join Date
Jan 2013
Posts
3
Rep Power
0

## Re: setting counter in craps game....

Sorry I did not post that correctly

Java Code:
```public class Craps
{

public static void main(String[] args)
{
int roll1 = (int)((Math.random()*6)+ 1);
int roll2 = (int)((Math.random()*6)+ 1);
int sum = roll1 + roll2;
int point = sum;
int win = 0;
int loss = 0;

for (int i = 0; i<=99; i++)
if ((sum == 7) || (sum == 11))
{
win++;
}
else if ((sum == 2)||(sum == 3)||(sum == 12))
{
loss++;
}
while ((sum != 7)&&(sum != point))
{
roll1 = (int)((Math.random()*6)+ 1);
roll2 = (int)((Math.random()*6)+ 1);
sum = roll1 + roll2;
}

if (sum == point)
{
win++;
}
else
{
loss++;
}
int probability = win/(win + loss);
System.out.println("The probability of winning is: " + probability);
}

}```  Reply With Quote

4. ## Re: setting counter in craps game....

Do you really write code without indentation? Go through Code Conventions for the Java Programming Language: Contents

db  Reply With Quote

5. Member Join Date
Jan 2013
Posts
3
Rep Power
0

## Re: setting counter in craps game....

Java Code:
```

public class Craps
{

public static void main(String[] args)
{
int roll1 = (int)((Math.random()*6)+ 1);
int roll2 = (int)((Math.random()*6)+ 1);
int sum = roll1 + roll2;
int point = sum;
int win = 0;
int loss = 0;

for (int i = 0; i<=99; i++)
if ((sum == 7) || (sum == 11))
{
win++;
}
else if ((sum == 2)||(sum == 3)||(sum == 12))
{
loss++;
}
while ((sum != 7)&&(sum != point))
{
roll1 = (int)((Math.random()*6)+ 1);
roll2 = (int)((Math.random()*6)+ 1);
sum = roll1 + roll2;
}

if (sum == point)
{
win++;
}
else
{
loss++;
}
int probability = win/(win + loss);
System.out.println("The probability of winning is: " + probability);
}

}```  Reply With Quote

6. ## Re: setting counter in craps game....

Thanks, that looks so much better, and is now readable. Now to your problem: Originally Posted by meesh I can not figure out why it is not counting right.
You need to tell us what result you expected, and what you got. And you could add some more System.out.println(...) calls to see the values of intermediate variables at various points in the program.

The java.util.Random class might be a better choice for generating a random int as it has a method that does exactly that, leading to more code clarity.

db  Reply With Quote

craps, loop counter 