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; 
        case SNAKE_EYES: 
        case TREY: 
        case BOX_CARS: 
            gameStatus = Status.LOST; 
            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; 
            }//end if statement

        }//end while        

    }//end method
}//end class

I am so confused now, could you please help me ???

6 Years
Discussion Span
Last Post by ~s.o.s~

enum Status{WIN, CONTINUE };
That's enough to define and initialise the enum, you don't need anything else.
What exactly was the comment that confused you?


When i write my code, in the if statement the compiler gives me an error. It says that initialize enum.


When i write my code, in the if statement the compiler gives me an error. It says that initialize enum

Whenever you declare a method scoped variable, you need to assign a value to it at least once before using it. In your code, the variable myStatus is not initialized before use. Either give it a default initial value or add an else to your existing if construct. Something like:

if (number != answer){ 
    myStatus = Status.CONTINUE;
} else {
    myStatus = Status.WIN;

You might also want to move the part where you accept user input inside the loop. Another good way of avoiding repetition would be to use a do...while loop.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.