Search code examples
javaif-statementlogical-operators

My current if statement with the logical II OR is having compile error


I am learning Java and following code is not working, can you please check and see what I did wrong? Thanks.

public class HelloWorld{

    public static void main(String []args){
        /*
         Ticket price: 10 normally

        Discounted price , 5 for:
        - Age 15 and under
        - Over the age 60
        - Students
        */
        int ticketPrice = 10;
        int age = 16;
        boolean isStudent = false;
        
        if( (age =< 15) || (age>60) ){
            ticketPrice = 5;
        }else if (isStudent){
            ticketPrice = 5;
        }
        System.out.println("The price is: "+ ticketPrice);
     }
}

Solution

  • It's <= (Less than or Equal to), not =<.

    public class HelloWorld
    {
        public static void main(String []args)
        {
            /*
            Ticket price: 10 normally
    
            Discounted price , 5 for:
            - Age 15 and under
            - Over the age 60
            - Students          */
        
            int ticketPrice = 10;
            int age = 16;
            boolean isStudent = false;
    
            if ( (age <= 15) || (age>60) || (isStudent) )
                ticketPrice = 5;
            System.out.println("The price is: "+ ticketPrice);
        }
    }