In the book i use, a craps game is the project. Here is the code:
import java.util.Random;
public class Craps {
private static final Random randomNumbers = new Random();
private enum Status { COUNTINUE, WIN, LOST };
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_ELEVEN = 11;
private static final int BOX_CARS = 12;
//plays one game of craps
public void play(){
int myPoint = 0; //point if no win or lost on first roll
Status gameStatus; //can contain CONTINUE, WIN or LOST.
int SumOfDice = rollDice(); //first roll of dice
switch (SumOfDice){
case SEVEN:
case YO_ELEVEN:
gameStatus = Status.WIN;
break;
case SNAKE_EYES:
case TREY:
case BOX_CARS:
gameStatus = Status.LOST;
break;
default:
gameStatus = Status.COUNTINUE;
myPoint = SumOfDice;
}//end switch
//while game is not complete
while ( gameStatus == Status.COUNTINUE ) // not WON or LOST.
{
SumOfDice = rollDice();
if (SumOfDice == myPoint ){
gameStatus = Status.WIN;
}else if ( SumOfDice == SEVEN ){
gameStatus = Status.LOST;
}//else if ends
}//while loop ends
if (gameStatus == Status.WIN){
System.out.println("You won the game, congruculations...");
}else System.out.println("You lost the game, please try again...");
}//method play ends
public int rollDice() {
int toplam;
int dice1 = 1 + randomNumbers.nextInt(6); //first dice roll
int dice2 = 1 + randomNumbers.nextInt(6); //second dice roll
toplam = dice1 + dice2;
System.out.printf("Player rolled %d + %d = %d\n", dice1, dice2, toplam);
return toplam; //return sum of dice
}//method rollDice ends.
}//end class 'Craps'
But here i tried to use what i have learned about enum, but it says initialize enum. In the example, i did not understand how it is initialized.
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class Rasgele1 {
public enum Status{WIN, CONTINUE };
public static void main(String[] args){
Random myRandom = new Random();
Scanner input = new Scanner(System.in);
Status myStatus;
int number = 1 + myRandom.nextInt(101);
int answer;
System.out.println("Please Enter a number...");
answer = input.nextInt();
if (number != answer){
myStatus = Status.CONTINUE;
}//end if
if (myStatus == Status.CONTINUE){
System.out.println("Please enter a number...");
number = input.nextInt();
}
while ( myStatus == Status.CONTINUE){
if ( answer > number ){
myStatus = Status.CONTINUE;
}else if ( answer < number ){
myStatus = Status.CONTINUE;
}else if ( answer == number ){
myStatus = Status.WIN;
break;
}//end if statement
}//end while
}//end method
}//end class
I am so confused now, could you please help me ???