The for code is correct the idea of iterating 7 times to request 7 different numbers, but then you have very specific errors.
1.- if(num== arreglo[num])
is asking if the number entered is equal to the position num which is incorrect, because if we enter the number 13 for example, we will be accessing position 13 of the array, but this only has 7 , which would give a IndexOutRangeException
.
2.- arreglo[num] = Convert.ToInt32 (Console.ReadLine());
the same thing happens, but now try to store the value of the entry in the position of the number entered which would give the previous exception.
The following steps could theoretically be carried out.
-Request the number and then iterate the elements to know if it is or is not
int[] arreglo = new int[7]; //creo el array
int num;
// Variable Bandera
bool seEncuentra = false;
//Capturar arreglo
for (int i = 0; i < 7;)
{
//Inicializamos en noSeEncuentra
seEncuentra = false;
Console.Write("Ingresa numero ");
//Obtenemos el número
num = Convert.ToInt32(Console.ReadLine());
//Iteramos sobre el array para verificar que no se encuentre
for(int j=0;j<arreglo.Length && !seEncuentra; j++)
{
// si es igual , se encuentra cambiamos la bandera
if (arreglo[j] == num) seEncuentra = true;
}
//si se encuentra
if (seEncuentra)
{
//regresamos y mostramos el mensaje respectivo
Console.Write("Número Ingresado ya existe ");
}
else
{
//caso contrario añadimos el valor al array
// e incrementamos el numero de eementos ingresados
// i++ .
arreglo[i] = num;
i++;
}
}
Of course, this for can be simplified and use contains
to verify if the element exists in the array.
int[] arreglo = new int[7]; //creo el array
int num;
//Capturar arreglo sin incrementar el i ,
// seincrementará cuando encuentre un número no repetido
for (int i = 0; i < 7; )
{
Console.Write("Ingresa numero ");
num = Convert.ToInt32(Console.ReadLine());
// Verificamos si existe en el array
if (!arreglo.Contains(num))
{
arreglo[i] = num;
i++;
}
else
{
Console.Write("Es un numero repetido");
}
}