Error in IMC code

Asked

Viewed 126 times

-6

I have the code below, which calculates BMI, you can tell me his mistake?

Follows the code:

import java.util.Scanner;
public class Paciente {

        public static double calcularIMC(double P,double A){
            double imc;
            imc=P/(A*A);
            return imc;

    }
        public static String diagnostico(){
            double A,P,imc;
            Scanner entrada=new Scanner(System.in);

            System.out.print("Entre com o valor de sua altura em metros :");
            A=entrada.nextInt();
            System.out.print("Entre com o valor de seu peso em Kg");
            P=entrada.nextInt();
            imc=Paciente.calcularIMC(P,A);

            if (imc<16.0){
                System.out.println("Baixo peso muito grave");
            }
            if (16<imc<16.99){
                System.out.println("Baixo peso grave");
            }
            if (17<imc<18.49){
                System.out.println("Baixo peso");

            }
            if (18.50<imc<24.99){
                System.out.println("Peso norma");
            }
            if(25<imc<29.99){
                System.out.println("Sobrepeso");
            }
            if(30<imc<34.99){
                System.out.println("Obesidade grau I");
            }
            if(35<imc<39.99){
                System.out.println("Obesidade grau II");
            }
            if(imc>=40){
                System.out.println("Obesidade grau II(obesidade morbida");
  • 1

    What mistake it makes?

  • Friend specify your mistake, nobody here is soothsayer. Consider editing the question and asking what error you make when trying to do this calculation, and taking a look at the help area http://answall.com/help/how-to-ask to create questions with better formatting and more specific.

1 answer

2

Your code has several semantic errors, the correct way to compare is not 16<imc<16.99, and yes 16< imc && imc <16.99, are two comparisons being made at the same time.

I improved on some things, like the method main that you changed the name, and without it your class would not start. However, there is much to improve, maybe with more studies, you improve and repair, or providing more information in the question.

public class Paciente {

    public static double calcularIMC(double P,double A){
            return P/(A*A);
    }

    public static void diagnostico(double imc){

            if (imc < 16.0){
                System.out.println("Baixo peso muito grave");
            }
            if (16< imc && imc <16.99){
                System.out.println("Baixo peso grave");
            }
            if (17<imc && imc <18.49){
                System.out.println("Baixo peso");

            }
            if (18.50< imc && imc < 24.99){
                System.out.println("Peso norma");
            }
            if(25<imc && imc <29.99){
                System.out.println("Sobrepeso");
            }
            if(30<imc && imc <34.99){
                System.out.println("Obesidade grau I");
            }
            if(35<imc && imc <39.99){
                System.out.println("Obesidade grau II");
            }
            if(imc>=40){
                System.out.println("Obesidade grau II(obesidade morbida");
            }
        }

    public static void main (String[] args) {
            double A,P, imc;
            Scanner entrada=new Scanner(System.in);

            System.out.println("Entre com o valor de sua altura em metros :");
            A = entrada.nextDouble();
            System.out.println("Entre com o valor de seu peso em Kg");
            P = entrada.nextDouble();
            diagnostico(calcularIMC(P,A));
    }
}

See working on IDEONE

Browser other questions tagged

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