Number repeated on matrix

0

I would like to know how I could do it so that when the user enters two equal numbers in the bet, I throw error that can not repeat two numbers in the same bet:

static bool Agregar(int[,] m, ref int tope) //agregar una apuesta
{
    bool disponible = true;
    bool repetido = true;
    int i = 0;
    while (i < 5)
    {
        try
        {
            Console.Write("\n" + "INGRESE NÚMERO (" + (i + 1) + ") : ");
            m[tope, i] = Convert.ToInt32(Console.ReadLine());
            repetido = true;
            for (int x = 0; x < tope; x++)
            {
                if (m[tope, i] == m[tope, i])
                {
                    i++;
                }
            }
            if (repetido)
            {
                Console.WriteLine("repe");
            }
            disponible = true;
            if (m[tope, i] >= 1 && m[tope, i] <= 48 && disponible == true)
            {
                i++;
            }
            else
            {
                disponible = false;
                Console.WriteLine("Número no disponible para apostar (1-48)");
            }
        }
        catch
        {
            Console.WriteLine("Número incorrecto");
        }
    }
    tope++;
    return disponible;
}


static void Main(string[] args)
{

    nombres = new string[cantidad];
        int[,] matriz = new int[cantidad, 5];
        i
                while (!salir)
                {
                    Console.Clear();
                    Console.WriteLine("----------------------");
                    Console.WriteLine("PRIMER OBLIGATORIO");
                    Console.WriteLine("----------------------");
                    Console.WriteLine("1- AGREGAR UNA APUESTA");
                    Console.WriteLine("2- AGREGAR UNA APUESTA SORPRESA");
                    Console.WriteLine("3- ELIMINAR UNA APUESTA");
                    Console.WriteLine("4- NÚMEROS DE UN APOSTADOR");
                    Console.WriteLine("5- LISTADO COMPLETO DE APUESTAS");
                    Console.WriteLine("6- NÚMEROS QUE NO HAN ESTADO EN APUESTAS");
                    Console.WriteLine("7- SALIR");
                    try
                    {
                        Console.Write("INGRESE OPCIÓN (1-7) : ");
                        opcion = Convert.ToInt32(Console.ReadLine());
                    }
                    catch
                    {
                        Console.WriteLine("La opción es incorrecta (1-7)");
                    }
                    Console.Clear();
                    switch (opcion)
                    {
                        case 1:
                            Console.Clear();
                            Console.WriteLine("---------------------------------------------------------");
                            Console.WriteLine("                   AGREGAR UNA APUESTA");
                            Console.WriteLine("---------------------------------------------------------\n");
                            if (tope < nombres.Length)
                            {
                                    Console.Write("INGRESE NOMBRE : ");
                                    nombres[tope] = Console.ReadLine();
                                    if (nombres[tope] == "")
                                    {
                                        mensaje = mensaje + "Debe ingresar un nombre";

                                        if (mensaje == "")
                                        {
                                            Console.WriteLine("Nombre ingresado correctamente");
                                        }
                                        else
                                        {
                                            Console.WriteLine(mensaje);
                                        }
                                    }
                                    else 
                                    {
                                        Agregar(matriz, ref tope);
                                        Console.WriteLine();
                                        Console.WriteLine("Se agrego correctamente su apuesta");
                                    }                            
                            }
                            else
                            {
                                Console.WriteLine("No hay màs apuestas disponibes");
                            }
                            Console.ReadLine();
                            break;
....

What I try to do is wrong, just in case!

    
asked by Francop 28.06.2017 в 16:45
source

1 answer

2

Well, in the method that you put us you have several problems:

  • instead of saving directly to the array what the user has entered, you must put it in a temporary variable to check if it is repeated before inserting it: var numeroIntroducido = Convert.ToInt32(Console.ReadLine());
  • Once this is done, checking if the number already exists is simple. Simply, you compare the number entered with all the ones that exist in the array, something like this:

    repetido = false;
    for (int j=0;j<m.GetLength(1);j++)
    {
        if (m[tope,j]==numeroIntroducido)
        {
            repetido=true;
            break;
        }
    }
    
  • On the other hand, using try / catch to check if the input is correct is not good, it is better to use Int32.TryParse
  • With all this, the code to check if the number is repeated more or less would be like this:

    bool repetido=true;
    while (repetido)
    {
         Console.Write("\n" + "INGRESE NÚMERO (" + (i + 1) + ") : ");
         int numeroIntroducido;
         if (Int32.TryParse(Console.ReadLine(),out numeroIntroducido))
         {
               repetido = false;
               //en este bucle es donde se comprueba si el numero esta ya en el array
               for (int j=0;j<m.GetLength(1);j++)
               {
                     if (m[tope,j]==numeroIntroducido)
                     {
                         repetido=true;
                         break;
                     }
               }
               if(!repetido) 
               { 
                   m[tope, i]=numeroIntroducido;
               }
         }
         else
         {
             Console.WriteLine("Número incorrecto");
         }
    }
    

    You have to adapt the check of whether it is available and put it inside the loop that asks for the 5 elements.

        
    answered by 28.06.2017 в 17:39