Hi,
The following code is from a textbook. I cannot get it to work. Would someone be so kind as to look at the code as I am coming up with two errors. They are the two lines towards the bottom that read:
int die1 = 1 + randomNumbers.nextInt(6);
int die2 = 1 + randomNumbers.next(6);
I assumed that because this code was in a school textbook that it would be fine but that is not happening. It shows a successful build but when I try to run it it says no main class found. I add public static void main etc and then there are errors all over the place.
I thought perhaps someone's keen eye might spot that without having to go through all the errors when I add main. I am very, very new to Java and find the language twisted and verbose. C++ was much easier for me. Any ideas? (I no longer have this class but wanted to see what a craps program looked like.lol)
import java.util.Random;
/**
*
* @author Lynn
*/
public class Craps
{
private enum Status { CONTINUE, WON, LOST };
// create random number generator for use in method rollDice
private final static int SNAKE_EYES = 2;
private final static int TREY = 3;
private final static int SEVEN = 7;
private final static int YO_LEVEN = 11;
private final static int BOX_CARS = 12;
public void play()
{int myPoint = 0;
Status gameStatus;
int sumOfDice = rollDice();
switch(sumOfDice){
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus=Status.LOST;
break;
default:
gameStatus = Status.CONTINUE;
myPoint = sumOfDice;
System.out.printf("Point is %d\n", myPoint);
break;
}
while (gameStatus==Status.CONTINUE)
{
sumOfDice = rollDice();
if (sumOfDice==myPoint)
gameStatus = Status.WON;
else
if (sumOfDice==SEVEN)
gameStatus = Status.LOST;
}
if (gameStatus==Status.WON)
System.out.println("Player wins");
else
System.out.println("Player loses");
}
public int rollDice()
{
int die1 = 1 + randomNumbers.nextInt(6);
int die2 = 1 + randomNumbers.next(6);
int sum = die1 + die2;
System.out.printf("Player rolled %d + %d = %d\n", die1,die2,sum);
return sum;
}
}