IF ELSE CONDITION IN JAVA QUESTION SOLVE (OFFICIAL HARD QUESTION SOLVE)

Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.
if employee is female, then she will work only in urban areas.

if employee is a male and age is in between 20 to 40 then he may work in anywhere

if employee is male and age is in between 40 t0 60 then he will work in urban areas only.

And any other input of age should print "ERROR"




Solution:


package if_else_control;

import java.util.Scanner;

public class ConditionDemo {
   
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
       
        char gender,ms;
        int age;
       
        System.out.print("Enter your age : ");
        age=input.nextInt();
       
        System.out.print("Enter your Gender : ");
        gender=input.next().charAt(0);
       
        System.out.print("Enter your Ms : ");
        ms=input.next().charAt(0);
       
        if(gender=='F' || gender=='f'){
            System.out.println("you will work only in urban areas");
       
        }
        else if(gender=='M' && age>=20 && age<=40){
            System.out.println("you may work in anywhere");
        }
       
        else if(gender=='M' && age>40 && age<=60){
            System.out.println("you will work in urban areas only.");
        }
        else{
            System.out.println("ERROR");
        }
        
    }
   
}





How to use Logic Oparetor "And" in java

Comments