I wrote a simple code for a craps game. I'm still figuring out how to work out the looping glitches, but it is giving me a java.lang.StackOverflowError at line 33:
//display the value of dice
System.out.println("You rolled " + die + "+ " + die2 + ". Totalling " + dice);.
Here is the code:
/**
* A simulation of the craps betting game.
*
* @
* @
*/
import java.util.Random;
public class Craps
{
//random number generator
private Random randomNumber = new Random();
//roll the dice
public int rollDice()
{
//first die
int die = (randomNumber.nextInt(6) + 1);
//second die
int die2 = (randomNumber.nextInt(6) + 1);
//add die + die2
int dice = die + die2;
//point value
int myPoint = 0;
//display the value of dice
System.out.println("You rolled " + die + "+ " + die2 + ". Totalling " + dice);
if (dice == 7) {
System.out.println ("Congratulations, YOU WIN!");
}
if (dice == 11) {
System.out.println ("Congratulations, YOU WIN!");
}
if (dice == 2) {
System.out.println ("I'm sorry, you lose.");
}
if (dice == 3) {
System.out.println ("I'm sorry, you lose.");
}
if (dice == 12) {
System.out.println ("I'm sorry, you lose.");
}
else {
System.out.println ("And the game continues...");
myPoint = dice;
if (dice == 7) {
System.out.println ("I'm sorry, you lose.");
}
while (dice != 7)
{
rollDice();
if (dice == myPoint) {
System.out.println ("Congratulations, YOU WIN!");
}
else {
myPoint = dice;
}
}
}
return dice;
}
}