dimanche 14 février 2021

Why this If-else condition is not working?

public static void bootUp(){

        System.out.println("Please select your activity or press E to exit");
        System.out.println("1. Driver Door");
        System.out.println("2. Remaining Door");
        System.out.println("3. Sensors");
        System.out.println("4. Radio Remote Control");

        boolean run = true;
        while(run){
            int userSelection;

            Scanner userInput = new Scanner(System.in);
            if(userInput.hasNextInt()){
                userSelection = userInput.nextInt();

                if(userSelection == 1){
                    driverDoor();
                }else if(userSelection == 2){
                    remainingDoors();
                }else if(userSelection == 3){
                    sensors();
                }else if(userSelection == 4){
                    radioRemoteControl();
                }
            }else if(userInput.hasNext()){
                String exit = userInput.next();
                if(exit.equalsIgnoreCase("e")){
                    run = false;
                }else{
                    System.out.println("Invalid key press E to exit.");
                }
            }
        }
}
public static void driverDoor(){

        System.out.println("**Driver Door**");
        System.out.println("1.1 Window Activity DD");
        System.out.println("1.2 Window Activity PD");
        System.out.println("1.3 Window Activity RR");
        System.out.println("1.4 Window Activity RL");
        System.out.println("Please select the activity or press B to return to back menu. or press E to exit");

        boolean ddActivity = true;
        while(ddActivity){
            float userSelection;
            Scanner userInput = new Scanner(System.in);
            if(userInput.hasNextFloat()){
                userSelection = userInput.nextFloat();
                if(userSelection == 1.1){
                    System.out.println("1.1 window activity DD");
                }else if(userSelection == 1.2){
                    System.out.println("1.2 Window Activity PD");
                }else if(userSelection == 1.3){
                    System.out.println("1.3 Window Activity RR");
                }else if(userSelection == 1.4){
                    System.out.println("1.4 Window Activity RL");
                }
            }else if(userInput.hasNext()){
                String exit_back = userInput.next();
                if(exit_back.equalsIgnoreCase("e")){
                    ddActivity = false;
                }else if (exit_back.equalsIgnoreCase("b")){
                    bootUp();
                }
            }
        }
}

Question 1. I have written two methods where the idea is clear. The user will select one option from the given four. Once the user selects one option it will lead him to the child option of whichever he has selected. For example, if the user selects Driver Door then there are 4 child options of Driver Door, now if the user selects any of the child options to let's say 1.1 Window Activity DD then the further process will proceed. But in this case, my code seems to be not working.

Question 2 Now, if the user commands wrong input in the case of child-activity selection then our program must send the message that "Please select the activity or press B to return to the back menu. or press E to exit" and after pressing E or B or respected child option (1.1/1.2...) it should work accordingly.

Aucun commentaire:

Enregistrer un commentaire