简体   繁体   中英

if one condition is satisfied it is not checking any other condition in if else

After one condition satisfied it is not checking other condition. I want that it shouls check for premium condition also

   public Car getCar(String CarType , String InsuranceType) //, //String InsuranceType)
   {
        // comparing string with method argument
     
        
    if(CarType.equalsIgnoreCase("Hatchback"))
        {
        return new Hatchback();
        }
    else if(CarType.equalsIgnoreCase("Sedan"))
        {
        return new Sedan();
        
        }
    else if(CarType.equalsIgnoreCase("SUV"))
        {
        return new SUV();
        }
    
    if(InsuranceType.equalsIgnoreCase("Premium"))
    {
        return new Premium();
    }
    return null;
      }

That's because you return when the if is true , so you don't get to the Premium check. The return keyword finished the execution of a method, and can be used to return a value from a method.

If you want that the Premium check will be executed, move it to be the 1st check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM