vendredi 16 février 2018

Need some help regarding an if-else piece of code

I have a piece of code I'm working with, that involves checking a condition and moving backwards to re-enter information if an invalid input is made. Please find the code, below:

Gender:
Console.WriteLine("Select Gender"
    +"\n (M)ale/(F)emale");
input = Console.ReadLine();

if (input=="M" || input==("m"))
    commonobj.gender = 1;

else if (input != ("F")||input!="f")
{
    Console.WriteLine("Invalid input, please enter again");
    goto Gender;
}

Intended behaviour: If the user inputs 'M' or 'm', the value of the object variable is changed. If the user inputs 'F' or 'f', the value of the object variable is unchanged. If the user inputs any other value, an error statement should be displayed and the user asked to re-enter the information.

Variables used: input - local variable, type String | commonobj.gender - object variable

Current behaviour: Entering 'F' or 'f' on the console displays the code under the 'else if' loop,

What am I doing wrong?

Note: The code executes as intended with 'm' being selected on the input.

Aucun commentaire:

Enregistrer un commentaire