Issue
I am new to Java and I was trying something and it won't work. I am trying to get input from the user for only male or female and the code builds, but whenever I enter the input M or F, it automatically gives the else "You did not choose M or F"
System.out.println("Please specify you're gender with M for male and F for female");
char gender = readertext.next().charAt(0);
char M = 0;
char F = 0;
if (gender == M) {
M = 1;
System.out.println("You picked M");
} else if (gender == F) {
F = 1;
System.out.println("You picked F");
} else {
System.out.println("You did not choose M or F");
}
Solution
char gender = readertext.next().charAt(0);
if (gender == 'M') {
System.out.println("You picked M");
} else if (gender == 'F') {
System.out.println("You picked F");
} else {
System.out.println("You did not choose M or F");
}
Answered By - Roslan Amir
Answer Checked By - David Goodson (JavaFixing Volunteer)