Read Two Notes and Make Media If They Are Valid Notes

Asked

Viewed 33 times

0

I’m having trouble solving the exercise below

inserir a descrição da imagem aqui

Shall I wear Desvio Condicional

And by my "thoughts" I believe I should use the Desvio Encadeado

I know that both grades must meet the criteria of having values between 0 and 10, and only then can I average! I thought to use only 'IF' (which will be in case it meets the "requirements" and 'ELSE' to display the message if one of the notes is not valid!

Am I thinking correctly? And if so, that I haven’t been able to "identify" what will be the conditions I should put in the IF regarding the notes!

  • Carol just a comment regarding your questions. If you have thought of a way to solve it and even what instructions to use, why not put your attempts at resolution ? This helps anyone who wants to answer because they understand their real difficulty.

  • Eh that usually when I come to ask I have no resolution made! Ai can not and I end up asking!

1 answer

0


There is no mystery! One compound deviation may be sufficient, unless you have in mind something more sophisticated.

public class Average {
    public static void main(String[] args) {
        double nota1 = 0, nota2 = 10, media;
        // Verifique se as duas notas obedecem o critério...
        if((nota1 >= 0 && nota1 <= 10) && (nota2 >= 0 && nota2 <= 10)) {
            // ... Se sim, calcule a média
            media = (nota1 + nota2)/2;
            System.out.printf("%.2f", media); // Perceba o uso de printf para dar precisão ao valor de output
        } else {
            System.out.println("Uma das notas é negativa ou maior que 10.");
        }
    }
}

If you change the initial values of the variables, you will see that the above code, simple, meets the challenge requirements.

  • That’s exactly what I was thinking, I just didn’t know if I only used If or Else If!

Browser other questions tagged

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