[英]“The left-hand side of an assignment must be a variable”
I get an error saying "The left-hand side of an assignment must be a variable" where is says else (itemNumber >=15) 我收到一个错误消息:“分配的左侧必须是一个变量”,其他地方说(itemNumber> = 15)
import java.util.Scanner;
public class Ch3Asg
{
public static void main(String[] args)
{
// Variables
Scanner input = new Scanner(System.in);
int itemNumber = 0;
double shippingCost = 0;
// Items Purchased
System.out.println("How many items did you purchase? ");
itemNumber = Integer.parseInt(input.nextLine());
// One Item Purchased
if ( itemNumber == 1 )
{
shippingCost = 2.99;
}
// 2-5 Items Purchased
else if ( itemNumber >= 2 && itemNumber <= 5 );
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1);
}
// 5-15 Items Purchased
if ( itemNumber > 5 && itemNumber < 15)
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5);
}
// More Than 15 Items Purchased
else ( itemNumber >= 15 )
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5)
+ .99 * (itemNumber - 14);
}
// Display Cost
System.out.printf("Shipping Cost is: $%.2f", shippingCost);
}
}
else (boolean statement)
is meaningless. else (boolean statement)
是没有意义的。 I think that you forgot the if: 我认为您忘记了以下情况:
else ( itemNumber >= 15 )
should be 应该
else if ( itemNumber >= 15 )
Or else if it represents the last and default option, it could be simply: 否则,如果它代表最后一个默认选项,则可能很简单:
else {
//..
}
else
construct does not take an expression, so else
结构不接受表达式,因此
else ( itemNumber >= 15 )
is syntactically wrong. 在语法上是错误的。
You need to use else if
instead 您需要使用else if
代替
else if ( itemNumber >= 15 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.