我不知道是什么导致了这个错误。我和我的老师检查了一遍,找不到问题所在。
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);
}
}
它在编译时抛出“需要常量表达式”错误,它指向 case p: 行,但是,如果我切换它们,它也会为 f: 抛出它,所以我必须完全关闭它们。