dimanche 19 novembre 2017

(Java) String declared in an 'if-else' statement cannot be resolved to a variable

I know that variables are 'contained' within if-else statements if they are declared there, and can't be used outside the statement. But how do I declare one outside, then change its String within the if-else?

System.out.println("Is it day or night?");
String dayornight = scan.nextLine();

if(dayornight.equals("day")) {
    String brightness = "bright";   

} else if(dayornight.equals("night")) {
    String brightness = "dark";

}
System.out.println("Then it must be " + brightness + " outside." );

}

Aucun commentaire:

Enregistrer un commentaire