Repeated hunch

Asked

Viewed 38 times

0

Hey, here’s my problem. I’m developing an application in c# (console) to learn c# and I’m having a problem that I can’t solve.

First I draw the numbers that will be drawn:

Random rnd = new Random();
        int[] numeros = { rnd.Next(1, 49), rnd.Next(1, 49), rnd.Next(1, 49), rnd.Next(1, 49), rnd.Next(1, 49), rnd.Next(1, 49) };

Then I show the numbers and order them (show on the console only for me to do tests):

Array.Sort(numeros);
        Console.WriteLine("Números sorteados: ");
        foreach (int i in numeros) Console.Write(i + " ");

Now I ask for the guess and create an array to receive the same:

Console.WriteLine("Insere o teu palpite de 6 números: ");
        int[] palpites = { int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()), int.Parse(Console.ReadLine()) };

The following code is where I see how many combinations are made by comparing the numbers of the arrays:

int combinacao = 0;
        if (palpites[0] == numeros[0] || palpites[0] == numeros[1] || palpites[0] == numeros[2] || palpites[0]== numeros[0] || palpites[0] == numeros[4] || palpites[0] == numeros[5])
        {
            combinacao = combinacao + 1;
        }
        if(palpites[1] == numeros[0] || palpites[1] == numeros[1] || palpites[1] == numeros[2] || palpites[1] == numeros[3] || palpites[1] == numeros[4] || palpites[1] == numeros[5])
        {
            combinacao = combinacao + 1;
        }
        if (palpites[2] == numeros[0] || palpites[2] == numeros[1] || palpites[2] == numeros[2] || palpites[2] == numeros[3] || palpites[2] == numeros[4] || palpites[2] == numeros[5])
        {
            combinacao = combinacao + 1;
        }
        if (palpites[3] == numeros[0] || palpites[3] == numeros[1] || palpites[3] == numeros[2] || palpites[3] == numeros[3] || palpites[3] == numeros[4] || palpites[3] == numeros[5])
        {
            combinacao = combinacao + 1;
        }
        if (palpites[4] == numeros[0] || palpites[4] == numeros[1] || palpites[4] == numeros[2] || palpites[4] == numeros[3] || palpites[4] == numeros[4] || palpites[4] == numeros[5])
        {
            combinacao = combinacao + 1;
        }
        if (palpites[5] == numeros[0] || palpites[5] == numeros[1] || palpites[5] == numeros[2] || palpites[5] == numeros[3] || palpites[5] == numeros[4] || palpites[5] == numeros[5])
        {
            combinacao = combinacao + 1;
        }

Last but not least I reveal the result:

if(combinacao == 0)
        {
            Console.Write("Não acertou nenhum número.");
        }
        else if(combinacao == 1)
        {
            Console.Write("Acertou um número.");
        }
        else
        {
            Console.Write("Acertou " + combinacao + " números.");
        }

Now comes my problem!

If I repeat my guess several times and that same number is drawn only once it will always count the times I give my guess.

console

It’s probably something quite simple but as I am very beginner I still can not understand, thank you all for the help!

1 answer

1

The easiest way is to check each entry, in a loop, and only add to the list after checking that the number is not repeated:

Console.WriteLine("Insere o teu palpite de 6 números: ");

List<int> palpites = new List<int>();

while(palpites.Length < 6)
{
    int palpite = int.Parse(Console.ReadLine());

    if(palpites.Contains(palpite))
    {
        Console.WriteLine($"O número {palpite} já foi utilizado") ;
    } 
    else
    {
        palpites.Add(palpite);
    } 
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.