Java error method main is Too Complex

Asked

Viewed 77 times

-5

My code is displaying the following message "method main is Too Complex" when I use several conditions in if to analyze repetition of numbers. With less data in if it works, but I need all these comparisons.

//Verifica Repetições
if ((combinacao[0] != combinacao[1])
        && (combinacao[0] != combinacao[2])
        && (combinacao[0] != combinacao[3])
        && (combinacao[0] != combinacao[4])
        && (combinacao[0] != combinacao[5])
        && (combinacao[0] != combinacao[6])
        && (combinacao[0] != combinacao[7])
        && (combinacao[0] != combinacao[8])
        && (combinacao[0] != combinacao[9])
        && (combinacao[0] != combinacao[10])
        && (combinacao[0] != combinacao[11])
        && (combinacao[0] != combinacao[12])
        && (combinacao[0] != combinacao[13])
        && (combinacao[0] != combinacao[14])
        && (combinacao[0] != combinacao[15])
        && (combinacao[0] != combinacao[16])
        && (combinacao[0] != combinacao[17])
        && (combinacao[0] != combinacao[18])
        && (combinacao[0] != combinacao[19])
        && (combinacao[1] != combinacao[2])
        && (combinacao[1] != combinacao[3])
        && (combinacao[1] != combinacao[4])
        && (combinacao[1] != combinacao[5])
        && (combinacao[1] != combinacao[6])
        && (combinacao[1] != combinacao[7])
        && (combinacao[1] != combinacao[8])
        && (combinacao[1] != combinacao[9])
        && (combinacao[1] != combinacao[10])
        && (combinacao[1] != combinacao[11])
        && (combinacao[1] != combinacao[12])
        && (combinacao[1] != combinacao[13])
        && (combinacao[1] != combinacao[14])
        && (combinacao[1] != combinacao[15])
        && (combinacao[1] != combinacao[16])
        && (combinacao[1] != combinacao[17])
        && (combinacao[1] != combinacao[18])
        && (combinacao[1] != combinacao[19])
        && (combinacao[2] != combinacao[3])
        && (combinacao[2] != combinacao[4])
        && (combinacao[2] != combinacao[5])
        && (combinacao[2] != combinacao[6])
        && (combinacao[2] != combinacao[7])
        && (combinacao[2] != combinacao[8])
        && (combinacao[2] != combinacao[9])
        && (combinacao[2] != combinacao[10])
        && (combinacao[2] != combinacao[11])
        && (combinacao[2] != combinacao[12])
        && (combinacao[2] != combinacao[13])
        && (combinacao[2] != combinacao[14])
        && (combinacao[2] != combinacao[15])
        && (combinacao[2] != combinacao[16])
        && (combinacao[2] != combinacao[17])
        && (combinacao[2] != combinacao[18])
        && (combinacao[2] != combinacao[19])
        && (combinacao[3] != combinacao[4])
        && (combinacao[3] != combinacao[5])
        && (combinacao[3] != combinacao[6])
        && (combinacao[3] != combinacao[7])
        && (combinacao[3] != combinacao[8])
        && (combinacao[3] != combinacao[9])
        && (combinacao[3] != combinacao[10])
        && (combinacao[3] != combinacao[11])
        && (combinacao[3] != combinacao[12])
        && (combinacao[3] != combinacao[13])
        && (combinacao[3] != combinacao[14])
        && (combinacao[3] != combinacao[15])
        && (combinacao[3] != combinacao[16])
        && (combinacao[3] != combinacao[17])
        && (combinacao[3] != combinacao[18])
        && (combinacao[3] != combinacao[19])
        && (combinacao[4] != combinacao[5])
        && (combinacao[4] != combinacao[6])
        && (combinacao[4] != combinacao[7])
        && (combinacao[4] != combinacao[8])
        && (combinacao[4] != combinacao[9])
        && (combinacao[4] != combinacao[10])
        && (combinacao[4] != combinacao[11])
        && (combinacao[4] != combinacao[12])
        && (combinacao[4] != combinacao[13])
        && (combinacao[4] != combinacao[14])
        && (combinacao[4] != combinacao[15])
        && (combinacao[4] != combinacao[16])
        && (combinacao[4] != combinacao[17])
        && (combinacao[4] != combinacao[18])
        && (combinacao[4] != combinacao[19])
        && (combinacao[5] != combinacao[6])
        && (combinacao[5] != combinacao[7])
        && (combinacao[5] != combinacao[8])
        && (combinacao[5] != combinacao[9])
        && (combinacao[5] != combinacao[10])
        && (combinacao[5] != combinacao[11])
        && (combinacao[5] != combinacao[12])
        && (combinacao[5] != combinacao[13])
        && (combinacao[5] != combinacao[14])
        && (combinacao[5] != combinacao[15])
        && (combinacao[5] != combinacao[16])
        && (combinacao[5] != combinacao[17])
        && (combinacao[5] != combinacao[18])
        && (combinacao[5] != combinacao[19])
        && (combinacao[6] != combinacao[7])
        && (combinacao[6] != combinacao[8])
        && (combinacao[6] != combinacao[9])
        && (combinacao[6] != combinacao[10])
        && (combinacao[6] != combinacao[11])
        && (combinacao[6] != combinacao[12])
        && (combinacao[6] != combinacao[13])
        && (combinacao[6] != combinacao[14])
        && (combinacao[6] != combinacao[15])
        && (combinacao[6] != combinacao[16])
        && (combinacao[6] != combinacao[17])
        && (combinacao[6] != combinacao[18])
        && (combinacao[6] != combinacao[19])
        && (combinacao[7] != combinacao[8])
        && (combinacao[7] != combinacao[9])
        && (combinacao[7] != combinacao[10])
        && (combinacao[7] != combinacao[11])
        && (combinacao[7] != combinacao[12])
        && (combinacao[7] != combinacao[13])
        && (combinacao[7] != combinacao[14])
        && (combinacao[7] != combinacao[15])
        && (combinacao[7] != combinacao[16])
        && (combinacao[7] != combinacao[17])
        && (combinacao[7] != combinacao[18])
        && (combinacao[7] != combinacao[19])
        && (combinacao[8] != combinacao[9])
        && (combinacao[8] != combinacao[10])
        && (combinacao[8] != combinacao[11])
        && (combinacao[8] != combinacao[12])
        && (combinacao[8] != combinacao[13])
        && (combinacao[8] != combinacao[14])
        && (combinacao[8] != combinacao[15])
        && (combinacao[8] != combinacao[16])
        && (combinacao[8] != combinacao[17])
        && (combinacao[8] != combinacao[18])
        && (combinacao[8] != combinacao[19])
        && (combinacao[9] != combinacao[10])
        && (combinacao[9] != combinacao[11])
        && (combinacao[9] != combinacao[12])
        && (combinacao[9] != combinacao[13])
        && (combinacao[9] != combinacao[14])
        && (combinacao[9] != combinacao[15])
        && (combinacao[9] != combinacao[16])
        && (combinacao[9] != combinacao[17])
        && (combinacao[9] != combinacao[18])
        && (combinacao[9] != combinacao[19])
        && (combinacao[10] != combinacao[11])
        && (combinacao[10] != combinacao[12])
        && (combinacao[10] != combinacao[13])
        && (combinacao[10] != combinacao[14])
        && (combinacao[10] != combinacao[15])
        && (combinacao[10] != combinacao[16])
        && (combinacao[10] != combinacao[17])
        && (combinacao[10] != combinacao[18])
        && (combinacao[10] != combinacao[19])
        && (combinacao[11] != combinacao[12])
        && (combinacao[11] != combinacao[13])
        && (combinacao[11] != combinacao[14])
        && (combinacao[11] != combinacao[15])
        && (combinacao[11] != combinacao[16])
        && (combinacao[11] != combinacao[17])
        && (combinacao[11] != combinacao[18])
        && (combinacao[11] != combinacao[19])
        && (combinacao[12] != combinacao[13])
        && (combinacao[12] != combinacao[14])
        && (combinacao[12] != combinacao[15])
        && (combinacao[12] != combinacao[16])
        && (combinacao[12] != combinacao[17])
        && (combinacao[12] != combinacao[18])
        && (combinacao[12] != combinacao[19])
        && (combinacao[13] != combinacao[14])
        && (combinacao[13] != combinacao[15])
        && (combinacao[13] != combinacao[16])
        && (combinacao[13] != combinacao[17])
        && (combinacao[13] != combinacao[18])
        && (combinacao[13] != combinacao[19])
        && (combinacao[14] != combinacao[15])
        && (combinacao[14] != combinacao[16])
        && (combinacao[14] != combinacao[17])
        && (combinacao[14] != combinacao[18])
        && (combinacao[14] != combinacao[19])
        && (combinacao[15] != combinacao[16])
        && (combinacao[15] != combinacao[17])
        && (combinacao[15] != combinacao[18])
        && (combinacao[15] != combinacao[19])
        && (combinacao[16] != combinacao[17])
        && (combinacao[16] != combinacao[18])
        && (combinacao[16] != combinacao[19])
        && (combinacao[17] != combinacao[18])
        && (combinacao[17] != combinacao[19])
        && (combinacao[18] != combinacao[19])
) {
  • The main point here is that you are not supposed to make 200 comparisons in it if. The solution is to go through the vector and compare and build the result

1 answer

5

This code does EXACTLY the same thing as the code that is written there, only using loops, you could implement this way in your code:

    boolean semRepeticoes = true;
    for (int i = 0; i < combinacao.length; i++)
    {
        for (int j = i + 1; j < combinacao.length; j++)
        {
            if (!(semRepeticoes = combinacao[i] != combinacao[j] || i == j))
                break;
        }
        if (!semRepeticoes)
            break;
    }
    if (semRepeticoes)
        System.out.println("não existem repetições");
    else
        System.out.println("Existem números repetidos no Array");
  • Thanks for the help, I’m layman in java yet. In this script you gave me it presents one of two messages, I know that will have repeated numbers. But I want him to print only those that are not repeated combinations

Browser other questions tagged

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