Avoid repeated in Matrix

-1

I edit my previous question, I have this code:

Methods:

class Program
{
    static void nuevo(int[] lista, int cantidad, ref int tope)
    {
        lista[tope] = cantidad;
        tope++;
    }

    static void jugada (int[,]m, int napostante) 
    {
        for(int col=0; col<m.GetLength(1);col++)
        {
            m[napostante,col] = Convert.ToInt32(Console.ReadLine());
        }
    }
}

And this in the main:

case 1:

    if (tope < nombre.Length)
    {

        Console.Write("Nombre: ");
        nombre[tope] = Console.ReadLine();

        Console.Write("Jugada : ");
        cargarjugada(matriz, tope);
        agregar(vector, cantidad, ref tope);
    }
    else
    {
        Console.WriteLine("No hay mas cupos");
        Console.ReadLine();
    }
    break;

How could I control that the numbers entered are not repeated and letters are not entered instead of numbers ?, this same I can apply then for a Random . It is structured programming and I can use comparators, variables, matrices and arrays.

    
asked by eleaefe 26.06.2017 в 16:09
source

1 answer

1

If you save the values obtained in an array you can do a while to continue taking numbers while the array contains that number. Something similar to this:

using System.Linq;

public class Loteria
{
    public void GuardarNumeros()
    {
        int[] numeros = new int[5];
        for (int i = 0; i < numeros.Length; i++)
        {
            int numero = ObtenerNumeroAleatorio();
            while (numeros.Contains(numero))
            {
                numero = ObtenerNumeroAleatorio();
            }
            numeros[i] = numero;
        }
    }
}

Also I can not find in your code where you do the random to get the number randomly.

The problem mainly comes from your use of Random (), explanation in English here . When calling the Random () constructor, it uses information from the computer, the clock, to randomly generate the numbers, a kind of seed. As the process is so fast, in the 5 laps you are using the same seed and therefore the random number is always the same.

If you take the constructor out of the for, the probability of obtaining the same random number will be less.

Random numeros = new Random();
for (int sorpresa = 0; sorpresa <= 4; sorpresa++)
{
    Console.Write(numeros.Next(0, 48));
}

You have totally changed the question with your last modification:

First of all to check that you can not enter letters, you can use the TryParse function. The TryParse function exists in all types of variables and passing a string to it, returns true or false if it could be converted, and if converted, removes the transformed variable from the function. What you would need to do is something like this:

string valorIntroducido = LeerValor();
int valorComoEntero;
while (!int.TryParse(valorIntroducido, out valorComoEntero))
{
    valorIntroducido = LeerValor();
}

To avoid repetitions in the matrix you should do what I explained before, only instead of Contains, you would have to go through each index of the array asking if that index of the array is equal to the entered value. You should use a control variable set to "false". If any aray index contains that value, you set the variable to "true". Once you have finished traversing the array, if the variable is "true", return to get another number, if it is "false", insert it because it did not exist in the matrix.

    
answered by 26.06.2017 в 16:23