mardi 15 mai 2018

I have to input the same string twice to get 'else if' code to run

I am required to use if else-if else statements within javascript (honestly not my first choice) all works properly except all the statements that come second always need to be entered twice.

i.e.: "Enter A or B." A works on first enter. B must be entered twice.

public static void exampleMethod(){
 if(input.next().equals("A")){
        System.out.println("This is said when A is input.");
    }
    else if(input.next().equals("B")){
        System.out.println("This is said when B is input");
        }
    else    
        exampleMethod(); //returns to top of method; 
}

Otherwise the code works fine, and everything under "B" does run after entering twice, but I need it to work so that it registers B when it's entered the first time and not having to enter "B" then "B" again to have it run. This also happens with switches like:

switch(input.next()){
   case "A":
    System.out.println("A pressed");
    break;
   case "B":
    System.out.println("B pressed");
    break;
  default:
    exampleMethod();
 }

Is this one of those annoying javascript moments? Because I've done something similar with C# and it works fine.

Aucun commentaire:

Enregistrer un commentaire