이 오류의 원인을 잘 모릅니다. 선생님과 나는 그 일을보고 잘못 된 것을 발견하지 못했습니다. 그러나, 그것은 또한 f를 위해 그것을 던져, 행 :상수 표현식이 필요합니까? (Java switch statement)
import java.util.Scanner;
public class MailAssignment
{
public static void main(String [] args){
Scanner userinput = new Scanner(System.in);
char p;
char f;
double price = 0;
System.out.println("First class or priority?");
char type = userinput.next().charAt(0);
System.out.println("How much does the package weigh? (in ounces)");
double weight = userinput.nextDouble();
switch (type){
case p:
if (weight > 16)
price = weight * 3.95;
else if (weight > 32)
price = (1.20 * (weight/16));
else
price = 3.50 * weight;
break;
case f:
if (weight < 1)
price = 0.34;
else if (weight > 1)
price = 0.34 + (weight * 21);
else if (weight > 13)
price = weight * 3.95;
else if (weight > 32)
price = 1.20 * (weight/16);
else
price = 3.50 * weight;
break;
}
System.out.println("Your price is: " +price);
}
}
그것은 오류가 컴파일 때와는 케이스 페이지를 가리키는 "필요한 상수 표현을"던졌습니다 나는 내가 완전히 꺼 일을해야을 전환하는 경우 .
'final'로 선언되지 않은 변수는 case 문에서 사용할 수 없습니다. –
나는 비슷한 것을 시도해 보았습니다. 변수가 final 인 경우에도 동일한 오류가 발생합니다. –