Flipping heads or tails presupposes randomness, so your program needs to have a method that can randomly choose one of the Enum values you created. Java has a class called Random, that cares about generating random values for you. More specifically, it has this method here:
public int nextInt(int bound)
It will generate a random integer between 0 and the limit you specify in the parameter exclusively, that is, to generate a random number between 0 and 10, you must enter 11 in the parameter.
Another thing you need is a method that the compiler creates for you every time you use an Enum: values()
. This method returns an array of all Enum values in the order you declared them inside it:
Moeda.values()[0]; // vai retornar Moeda.CARA
Moeda.values()[1]; // vai retornar Moeda.COROA
Attention: this is unrelated to this variable indice
that you created. Here you are accessing position 0 and position 1 of the array. As you declared HEADS and then TAILS, the values
creates the array with CARA at the first position of the array (0) and COROA at the second position (1). Actually, this variable indice
is unnecessary.
Now it’s easy: you’ll generate a random number between 0 and 1, go to the value array, retrieve the value of Enum stored at that position and that’s it.
This light is enough for you to do your exercise. Break your head a little and, if you evolve, edit the code in your question, updating with what you got and, if you have any questions, comment on this answer here.