Returning different messages depending on the results of a [closed]

1

I have the following question:

I have a function that can give me 4 different values. I would like to create a message (string) for each value of the function, so that the message was released with its respective value, for example:

If the function returns B, I have message B, if the function returns the value A, I get the message A ...

code:

 Double N1 = N1(b, a, A, B, C, D);

 String resultado = (N1 == A && N1 < nM ? "Tipo A" : (N1 == B && N1 < nM ? "Tipo B": (N1 == C && N1 < nM ? "Tipo C" : (N1 == D && N1 < nM ? "Tipo D" : "Passa!"))));

The value of the function is N1, which can be A, B, C or D. In addition, the message "Type X" should only appear if N1 is less than double nM. If nM is less than N1, I should return the message "Pass!"

The problem is the message I get is always "Pass!" even when nM is greater than N1.

How do I proceed?

    
asked by anonymous 28.09.2017 / 20:28

1 answer

1

Try doing something like this:

Map<Double, String> mensagens = new HashMap<>(4);
mensagens.put(A, "Tipo A");
mensagens.put(B, "Tipo B");
mensagens.put(C, "Tipo C");
mensagens.put(D, "Tipo D");

Double escolhido = N1(b, a, A, B, C, D);
String resultado = nM > escolhido ? "Passa" : mensagens.get(escolhido);

However, you did not provide much information about the context of this code. I believe that with more information about the context of the problem, another better solution should exist. In particular, if you give more information about what they are, what they do and what they serve the variables N1 , nM , a , b , A , B C , types A, B, C and D and the "Pass". I recommend you read about the XY problem .

Also, make sure that there is no error within the D method.

    
29.09.2017 / 15:44