My program consists of the following:
We generate an array of 10 positions with random numbers from 1 to 20, then with a method called repeated we check that the numbers that have been generated are not in the array, in case they are on and get otherwise they do not get
This is the code:
import java.util.Random;
import java.util.Scanner;
public class pruebaexamen {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rant = new Random();
Scanner n = new Scanner(System.in);
// crear un array de numeros aleatoros mas pequeños que el 20 y luego en
// otro array mostrar los numeros mas pequeños que el 20 que no se hayan
// generado en el array
int[] array = new int[10];
int numero = 0;
// lo generamos
for (int i = 0; i < array.length; i++) {
numero = rant.nextInt(20 - 0);
if (repetido(array, numero) == false) {
array[i] = numero;
}
}
// comprovamos que no haya ningun numero repetidos
// lo mostramos
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
public static boolean repetido(int[] array, int valor) {
boolean repetido = false;
for (int i = 0; i < array.length; i++) {
if (array[i] == valor) {// si el valor generado aleatoriamente esta
// dentro del array le marcamos como true y
// por lo tanto no lo metera en el array
repetido = true;
} else if (array[i] != valor) {
repetido = false;
}
}
return repetido;
}
}
The situation is as follows: Even having done this, I keep creating the numbers with some repetitions, what have I failed to do? Is there any other way to do it or improve it?