我不知道是什么导致了这个错误。我的老师和我过去了,无法找到问题所在。需要常量表达式? (Java switch语句)
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);
}
}
它抛出错误时,它的编译和它所指向的情况下,P A“要求常量表达式”:行,但是,它也把它扔了对F:如果我切换他们,所以我必须做一些完全关闭。
你不能在'case'语句中使用变量,这些变量没有声明为final。 – 2013-02-26 14:13:46
我正在尝试类似的东西,事实证明,即使变量是最终的,也会抛出同样的错误。 – 2013-12-25 23:56:47