Incompatibilidade de tipo:não é possível converter de Random para int [fechado]
-
20-12-2019 - |
Pergunta
Como posso preencher esse array com inteiros aleatórios e evitar esse erro de incompatibilidade de tipo?Tentei escalar, mas não tive muito sucesso.Obrigado
public static int[] generateRandom(int n) {
Random r = new Random(1);
r.nextInt(Integer.MAX_VALUE);
int[] ranArray = new int[n];
for (int i = 0; i < n; i++) {
ranArray[i] = r;
}
printArray(ranArray);
return ranArray;
}
Solução
Você não pode atribuir Random
digite para int
for (int i = 0; i < n; i++) {
ranArray[i] = r; // Type mismatch
}
Em vez disso faça isso
for (int i = 0; i < n; i++) {
ranArray[i] = r.nextInt(Integer.MAX_VALUE);;
}
Outras dicas
r é do tipo Random
enquanto ranArray[i]
é inteiro e, portanto, o erro.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow