Read Two Notes and Make Media If They Are Valid Notes

1

I'm having trouble solving the exercise below

IshoulduseDesvioCondicional

Andbymy"thoughts" I think I should use Desvio Encadeado

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

Am I thinking correctly? And if yes, I could not "identify" what conditions I should put in the IF regarding the notes!

    
asked by anonymous 11.05.2018 / 00:04

1 answer

0

There is no mystery! A compound deviation may suffice, 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 simple code meets the challenge requirements.

    
11.05.2018 / 00:35