C sharp - como uso bien los array exist

 
Vista:
sin imagen de perfil

como uso bien los array exist

Publicado por charles132 (4 intervenciones) el 27/07/2021 19:40:24
bueno soy nuevo y necesito ayuda ya que tampoco soy muy experimentado en el mundo de la programación en fin como lo dice el titulo tengo problemas con el array exist
1. no se como hacer que en ves de que el usuario ingrese números directamente en el array quiero que se meta el numero en una variable luego el array exist confirme que ese numero no esta en el array el numero pasa a la variable y se repite pero en caso de que no simplemente se le debería escribir un texto al usuario en el que se diga algo como no puede insertar el numero bla porque ya existe en el array
2. justo ahí empieza mi segundo problema no se como usar el array exist en este caso estoy muy perdido ayuda

dejo muestra del codigo incompleta para que puedas ayudarme

static void Main(string[] args)
{
int x = int.Parse(Console.ReadLine());
Console.WriteLine("cuantos elementos quiere en el array :");
string[] name = new string[x];
int f = 1;
int t = 0;
while (t < x)
{
Console.WriteLine("escriba un numero para insertar en el array: ");
int h = int.Parse(Console.ReadLine());

Array.Exists(name, == h);
}

}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Santiago

como uso bien los array exist

Publicado por Santiago (24 intervenciones) el 21/08/2021 19:59:58
Hola:

Esta función te puede ayudar a ver wsi existe o no:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static bool Existe(string[] matriz, int valor)
        {
            // Recorremos la matriz de principio a fin
            for (int i = 0; i < matriz.Length; i++)
            {
                // Si existe, devolvemos "true"
                if (matriz[i] != null)
                {
                    if (int.Parse(matriz[i]) == valor)
                        return true;
                }
            }
 
            // No existe. Devolvemos "false"
            return false;
        }

No obstante, creo entender qué quieres hacer: añadir los que no existen hasta un total de "x" que indique el usuario, pero sin repetirlos. ¿Es correcto? Si es así, incluyo este código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Program
    {
        static void Main(string[] args)
        {
 
            Console.WriteLine("cuantos elementos quiere en el array :");
            int x = int.Parse(Console.ReadLine());
 
            string[] name = new string[x];
            int f = 0;
            int t = 0;
            while (t < x)
            {
                Console.WriteLine("escriba un numero para insertar en el array: ");
                int h = int.Parse(Console.ReadLine());
 
                if (Existe(name, h))
                {
                    Console.WriteLine("Existe");
                }
                else
                {
                    Console.WriteLine("No existe");
                    name[t]= h.ToString();
                    t = t + 1;
                }
                Console.WriteLine("t=" + t.ToString());
            }
 
            Imprime(name);
            Console.ReadLine();
        }
 
        private static void Imprime(string[] matriz)
        {
            // Recorremos la matriz de principio a fin
            for (int i = 0; i < matriz.Length; i++)
            {
                Console.WriteLine(matriz[i]);
            }
 
        }
 
 
        private static bool Existe(string[] matriz, int valor)
        {
            // Recorremos la matriz de principio a fin
            for (int i = 0; i < matriz.Length; i++)
            {
                // Si existe, devolvemos "true"
                if (matriz[i] != null)
                {
                    if (int.Parse(matriz[i]) == valor)
                        return true;
                }
            }
 
            // No existe. Devolvemos "false"
            return false;
        }
    }

Intuyo que estás aprendiendo y esto debe ser un ejercicio, pero en estos casos es mejor usar "ArrayList" o "List".

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar