“非法开始表达”错误与else if

时间:2016-10-25 19:44:23

标签: java if-statement expression

我正在尝试在交换机中执行else if语句,但我不断收到“非法启动表达式错误”。错误通常会在< =在案例1中弹出。我尝试添加括号和括号以查看是否存在问题,但事实并非如此。

    import java.util.Scanner;

    public class InvoiceApp
    {
        public static void main(String[] args)
        {
            //Declare variables and Scanner object
            float subtotal;
            int customerType;
            double discountAmount;
            double discountPercent;
            double total;

            Scanner keyboard = new Scanner(System.in);

            //Prompt user for subtotal   
            System.out.println("Please enter the subtotal: ");

            //Read subtotal
            subtotal = keyboard.nextFloat();

            //Calculate Discount Rate
            switch(customerType) {
                case 1: if  (subtotal >= 500) discountPercent = 20;

                        else if (subtotal < 500 && <= 250) 
                            discountPercent = 15; 

                        else if (subtotal < 250 && <= 100) 
                            discountPercent = 10; 

                        else if (subtotal < 100) 
                            discountPercent = 0; 



            }               

2 个答案:

答案 0 :(得分:1)

if  (subtotal < 100) 
  discountPercent = 0;
else if (subtotal < 250) 
  discountPercent = 10;
else if (subtotal < 500) 
  discountPercent = 15;
else
  discountPercent = 20;

答案 1 :(得分:-1)

subtotal < 500 && subtotal<= 250