I'm trying to implement a method in which it randomly gets into an if. I implemented it this way:
public void mover(){
int escolha;
Random generator = new Random();
// Next room
do{
// Verify external door
if(this.minhaLocalizacao instanceof IPortaExterna){
IPortaExterna local = (IPortaExterna) minhaLocalizacao;
escolha = generator.nextInt(2);
if(escolha == 1){
setMinhaLocalizacao(local.getLocalPorta());
}
}
escolha = generator.nextInt(minhaLocalizacao.saidas.length - 1);
setMinhaLocalizacao(minhaLocalizacao.saidas[escolha]);
}while(!(this.minhaLocalizacao instanceof IEsconderijo));
}
The program can compile and run normally, but sometimes I receive the error message:
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.base/java.util.Random.nextInt(Random.java:557)
...
I know the method nextInt()
doesn't accept the argument 0 or negative numbers, but in this case I'm using 2 as the argument. I already tried with other numbers (for example 3), but sooner or later this same error appears. I can't understand why this error is happening.
Aucun commentaire:
Enregistrer un commentaire