Summarizing the result of several logical tests in a single result

Asked

Viewed 27 times

0

Good afternoon,

I have some strings that come from tests. Example:

            String teste2 = (nB < 0.0 && m1 <= j ? "OK" : "Não passa!");
            String teste3 = (nB < 0.0 && m2 <= j ? "OK" : "Não passa!");
            String teste4 = (nB > 0.0 && m2 <= 35.0 ? "OK" : "Não passa!");
            String teste5 = (nB > 0.0 && m2 <= 35.0 ? "OK" : "Não passa!");

I would like to summarize everything in a single string. In this case, if any of the tests showed the message "Don’t pass!" my summary string would return "Don’t pass!" otherwise it would return "OK". The problem is that with the following code it always returns "It doesn’t pass!".

String resultado = (teste2.equals("Não passa!") || teste3.equals("Não passa!") || teste4.equals("Não passa!") || teste5.equals("Não passa!") || teste6.equals("Não passa!") || teste7.equals("Não passa!") || teste8.equals("Não passa!") || teste9.equals("Não passa!") || teste10.equals("Não passa!") ? "Não passa!" : "OK");

What am I doing wrong? Grateful.

  • In the teste4, wasn’t meant to use m1 instead of m2?

  • That will never pass, because the nb < 0.0 of teste1 and teste2 contradicts the nb > 0.0 of teste3 and teste4.

  • Because you use Strings with "OK" and "Não passa!" together with the ternary operator and then goes out doing tests with equals instead of simply using the boolean directly?

1 answer

2

You can do it like this:

        boolean teste2 = nB < 0.0 && m1 <= j;
        boolean teste3 = nB < 0.0 && m2 <= j;
        boolean teste4 = nB > 0.0 && m1 <= 35.0;
        boolean teste5 = nB > 0.0 && m2 <= 35.0;

        // ...

        boolean resultado = teste2 && teste3 && teste4 && teste5
                && teste6 && teste7 && teste8 && teste9 && teste10;

Avoid using string-oriented programming. This is bad programming practice.

Also note that the condition nb < 0.0 of teste2 and of teste3 and the codition nb > 0.0 of teste4 and of teste5. There is no way these two conditions can be true simultaneously because nb cannot be greater and less than zero at the same time. Therefore, there is no way resultado be true.

So either some of these tests of yours must be checking for incorrect conditions or there’s some hole in the logic of what you’re testing.

Browser other questions tagged

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