我收到错误消息“分配的左侧必须是变量”,其他地方是else(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);
}
}
答案 0 :(得分:6)
else (boolean statement)
毫无意义。我想你忘记了if:
else ( itemNumber >= 15 )
应该是
else if ( itemNumber >= 15 )
否则,如果它代表最后一个默认选项,则可能只是:
else {
//..
}
答案 1 :(得分:0)
else
构造不接受表达式,所以
else ( itemNumber >= 15 )
在语法上是错误的。
您需要使用else if
代替
else if ( itemNumber >= 15 )