Cannot find Symbol - variable calories

Asked

Viewed 214 times

1

Why this mistake if I’m setting the calories on ifs?

import java.util.Scanner;
public class Alimentos {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.println("\nDigite o Alimento (Vegetariano, Peixe, Frango ou Carne): ");
        String alimento = in.nextLine();
        System.out.println("\nDigite a Bebida (Chá, Refri Diet, Suco de melancia ou Suco de Laranja): ");
        String bebida = in.nextLine();
        System.out.println("\nDigite a Sobremesa (Abacaxi, Sorvete, Sorvete Diet ou Mousse): ");
        String sobremesa = in.nextLine();
        int calorias = 350;
        int caloriasbebida = 100;
        int caloriassobremesa = 200;
    if(alimento.toLowerCase().equals("vegetariano")){
        int calorias = 180;
    } else if(alimento.toLowerCase().equals("peixe")){
        int calorias = 230;
    } else if(alimento.toLowerCase().equals("frango")){
        int calorias = 250;
    }

    if(bebida.toLowerCase().equals("chá")){
        int caloriasbebida = 20;
    } else if(bebida.toLowerCase().equals("refri diet")){
        int caloriasbebida = 65;
    } else if(bebida.toLowerCase().equals("suco de melancia")){
        int caloriasbebida = 70;
    }

    if(sobremesa.toLowerCase().equals("abacaxi")){
        int caloriassobremesa = 75;
    } else if(sobremesa.toLowerCase().equals("sorvete")){
        int caloriassobremesa = 170;
    } else if(sobremesa.toLowerCase().equals("sorvete diet")){
        int caloriassobremesa = 110;
    }
    int resultado = calorias+caloriasbebida+caloriassobremesa;
    System.out.println("Total de Calorias da Refeição: " +resultado);
}
}

1 answer

4


You are declaring the variables within the if then they only exist within it, when it comes out, they no longer exist. You need to learn about scope (this gives an introduction but to learn it would be good to follow all the concepts in a good book).

When a variable is declared within a scope, it ceases to exist when it leaves this scope. Where there is a block (usually enclosed by keys { }) you are creating a new scope. An internal scope to another scope "inherits" variables in the previous scope.

import java.util.Scanner;

class Alimento {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("\nDigite o Alimento (Vegetariano, Peixe, Frango ou Carne): ");
        String alimento = in.nextLine();
        System.out.println("\nDigite a Bebida (Chá, Refri Diet, Suco de melancia ou Suco de Laranja): ");
        String bebida = in.nextLine();
        System.out.println("\nDigite a Sobremesa (Abacaxi, Sorvete, Sorvete Diet ou Mousse): ");
        String sobremesa = in.nextLine();
        int calorias = 350;
        int caloriasbebida = 100;
        int caloriassobremesa = 200;
        if (alimento.toLowerCase().equals("vegetariano")) calorias = 180;
        else if (alimento.toLowerCase().equals("peixe")) calorias = 230;
        else if (alimento.toLowerCase().equals("frango")) calorias = 250;
        if (bebida.toLowerCase().equals("chá")) caloriasbebida = 20;
        else if (bebida.toLowerCase().equals("refri diet")) caloriasbebida = 65;
        else if (bebida.toLowerCase().equals("suco de melancia")) caloriasbebida = 70;
        if (sobremesa.toLowerCase().equals("abacaxi")) caloriassobremesa = 75;
        else if (sobremesa.toLowerCase().equals("sorvete")) caloriassobremesa = 170;
        else if (sobremesa.toLowerCase().equals("sorvete diet")) caloriassobremesa = 110;
        int resultado = calorias + caloriasbebida + caloriassobremesa;
        System.out.println("Total de Calorias da Refeição: " + resultado);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This code still has a problem if the user does not enter anything.

I optimized your original question code by initializing the variable calorias with 350, to kill all else.

I take this opportunity to say that it would be good to organize the code in a more standardized way. This helps to understand what is happening.

  • I tried to validate the variables as 0 before the if too! (I remembered that class a little after posting rsrs) instead of him changing the value within the ifs it returns this error: variable calories is already defined in method main he’s not changing the value :/

  • Because you tried to declare it several times. You can only declare a variable once within the scope. And the scope is nested. That is, the inner scopes inherit everything that has ever been declared in the outer scope, so what is trying to declared within the if cannot be the same as what has been declared out of it.

  • But I am trying to declare different values :/ edited the question with the code I am trying to compile and getting the above error. I started with 350 as Voce gave the hint and I’m trying to switch to different values of this

  • I have already added the entire program for Voce to understand the idea rsrs

  • @William avoid changing the question after posting unless it is to clarify further, you could invalidate the answers. So it’s always good to think carefully if you’ve already posted everything you need the first time. I posted the new version. The problem is the same, you cannot declare variables within the if who have already been declared out of it. You can use the variables declared outside of it, after all an internal scope inherits what exists in the external scope, but cannot declare again because it is already declared. To resolve simply withdraw the statement and only use it. Study on scope.

  • Got faltlo my :# Thank you so much for the support!

Show 1 more comment

Browser other questions tagged

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