Problem with methods

Asked

Viewed 69 times

0

I am making a program that calculates BMI, using methods without Return. Programme in question:

package pct;
import java.util.Scanner;
public class Ex02Metodos {
    public static void main(String[] args) {
        Scanner teclado = new Scanner(System.in);
        String sexo;
        double altura, peso, valorIMC = 0;


        System.out.println("Digite sua altura:");
        altura = teclado.nextDouble();
        System.out.println("Digite seu peso");
        peso = teclado.nextDouble();
        teclado.nextLine();
        System.out.println("\n\nDigite seu sexo:");
        sexo = teclado.nextLine();

        calcularimc(altura,peso,valorIMC,sexo);


    }
    public static void calcularimc(double altura, double peso, double valorIMC, String sexo){
        valorIMC = peso / (altura * altura);

        if(sexo == "mulher"){
            if(valorIMC < 19.1){
                System.out.println("Abaixo do peso");
            }
            else if(valorIMC == 19.1 & valorIMC <= 25.8 ){
                System.out.println("Peso normal");
            }
            else if(valorIMC == 25.8 & valorIMC <= 27.3){
                System.out.println("Marginalmente acima do peso");
            }
            else if(valorIMC == 27.3 & valorIMC <= 31.1){
                System.out.println("Acima do peso ideal");
            }
            else if(valorIMC > 31.1){
                System.out.println("Obeso");
            }
        }
        else if(sexo =="homem"){
            if(valorIMC < 20.7){
                System.out.println("Abaixo do peso");
            }
            else if(valorIMC == 20.7 & valorIMC <=26.4){
                System.out.println("Peso normal");
            }
            else if(valorIMC == 26.4 & valorIMC <= 27.8){
                System.out.println("Marginalmente acima do peso");
            }
            else if(valorIMC == 27.8 & valorIMC <= 32.3){
                System.out.println("Acima do peso ideal");
            }
            else if(valorIMC > 32.3){
                System.out.println("Obeso");
            }
        }
        }

    }

At the time of executing, is not running the method, it closes.

  • You tried to remove the public static void main() from within the scope of the class and rotate again?

  • 1

    How do you know he’s not running the method calcularimc?

  • Forgive my lack of knowledge on the subject, but how do I know? Why did not print anything on the screen after the input question still in the main method. The program ended as soon as asked for input on user sex.

  • The method is running yes, it’s just not going through if Else. I just don’t know why yet.

  • Ta working normally.

  • Yeah, it looks like if Else isn’t working.

Show 1 more comment

1 answer

5


Maybe the reason is to use the operator == to compare strings. They are objects, and should be compared with equals. Comparing with the operator == you are comparing the object reference(memory address) and not its content itself, as occurs using the equals class String, and even though the strings have the same content, they occupy different memory spaces(except for exceptions).

There are other logic problems as well, rather than using >= you were using == in the logic of some ifs. With the change below, it returns to function normally in all conditions:

public static void calcularimc(double altura, double peso, double valorIMC, String sexo) {
    valorIMC = peso / (altura * altura);

    if (sexo.equals("mulher")) {
        if (valorIMC < 19.1) {
            System.out.println("Abaixo do peso");
        } else if (valorIMC >= 19.1 & valorIMC <= 25.8) {
            System.out.println("Peso normal");
        } else if (valorIMC >= 25.8 & valorIMC <= 27.3) {
            System.out.println("Marginalmente acima do peso");
        } else if (valorIMC >= 27.3 & valorIMC <= 31.1) {
            System.out.println("Acima do peso ideal");
        } else if (valorIMC > 31.1) {
            System.out.println("Obeso");
        }
    } else if (sexo.equals("homem")) {
        if (valorIMC < 20.7) {
            System.out.println("Abaixo do peso");
        } else if (valorIMC >= 20.7 & valorIMC <= 26.4) {
            System.out.println("Peso normal");
        } else if (valorIMC >= 26.4 & valorIMC <= 27.8) {
            System.out.println("Marginalmente acima do peso");
        } else if (valorIMC >= 27.8 & valorIMC <= 32.3) {
            System.out.println("Acima do peso ideal");
        } else if (valorIMC > 32.3) {
            System.out.println("Obeso");
        }
    }
}
  • I did that, and even then, it seems that if Lse do not exist. Do not print on the screen my conditions. The program closes as soon as it asks user sex.

  • @zHardy see the edition

Browser other questions tagged

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