0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int codigo, quantidade;
double preco;
Scanner sc = new Scanner(System.in);
codigo = sc.nextInt();
quantidade = sc.nextInt();
sc.close();
switch (codigo) {
case 1:
preco = 4.0 * quantidade;
break;
case 2:
preco = 4.5 * quantidade;
break;
case 3:
preco = 5.0 * quantidade;
break;
case 4:
preco = 2.0 * quantidade;
break;
case 5:
preco = 1.5 * quantidade;
break;
}
System.out.printf("Total: R$ %.2f%n", preco);
}
}
In the code thingy System.out.printf("Total: R$ %.2f%n", preco);
indicates an error that in the boot snippet of the price variable double preco;
I need to initialize it with 0 double preco = 0
but why can’t I initialize it without the value 0? Indicates this error:
Exception in thread "main"
java.lang.Error: Unresolved Compilation problem:
The local variable preco may not have been initialized
at Main.main(Main.java:29)