Answer the question
In order to leave comments, you need to log in
How to randomly select an object?
Hello!
I wanted to make a Rock Paper Scissors console game.
user is given to choose one of three values, and the Computer chooses randomly one of three options: rock, scissors or paper.
but I don’t know how to implement random for a computer, how can I do this?
user code:
public void gameMethod(){
Scanner sc = new Scanner(System.in);
System.out.printf("Вбирете значение: \n 1. %s \n 2. %s \n 3. %s",
Values.ROCK, Values.PAPER , Values.SCISSORS);
int userChoice = sc.nextInt();
Random random = new Random();
}
Answer the question
In order to leave comments, you need to log in
public enum RockPaperScissors {
ROCK,
PAPER,
SCISSORS;
public RockPaperScissors getRandomValue() {
int random = (int) (Math.random() * RockPaperScissors.values().length);
return RockPaperScissors.values()[random];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question