How to take only one value of a Random variable and turn it into a constant?

Asked

Viewed 56 times

2

How do I get only one value from a variable you use Random and store in another variable? It is possible?

Wanted to store what will appear to the user

int cartaUnoPersonagem = new Random().nextInt(personagem.length);
System.out.println("Certo "+nome+", você é um(a):"+personagem[cartaUnoPersonagem]);

1 answer

2

Use the keyword final:

final int cartaUnoPersonagem = new Random().nextInt(personagem.length);
  • 1

    No need for parentheses, can use direct new Random().nextInt(personagem.length): https://ideone.com/MA9DaD

  • @Jeffersonquesado Thanks, I corrected the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.