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);
}
}
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 :/– GGirotto
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.– Maniero
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
– GGirotto
I have already added the entire program for Voce to understand the idea rsrs
– GGirotto
@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.– Maniero
Got faltlo my :# Thank you so much for the support!
– GGirotto