Hi! I recently started teaching Java to myself yesterday via TheNewBoston on youtube. I began working on a few programs from the programmingbydoing website but I'm stuck on this problem:http://programmingbydoing.com/a/gender-game.html
It's a problem focusing on nested if statements.
Here's my code!:
import java.util.Scanner ;
public class MainClass {
public static void main(String args []) {
String gender, last, first, status;
int age;
Scanner response = new Scanner(System.in);
System.out.print("What is your gender (M or F): ");
gender = response.nextLine();
System.out.print("First name: ");
first = response.nextLine();
System.out.print("Last name: ");
last = response.nextLine();
System.out.print("Age: ");
age = response.nextInt();
if (gender.equals('M')) {
if (age >= 20)
{System.out.printf("Then I shall call you Mr. %s.", last);}
else {System.out.printf("Then I shall call you %s %s.", first, last);}
}
else {
if (age >= 20){
System.out.printf("Are you married, %s (y or n)?", first);
status = response.nextLine();
if (status.equals('y')){
System.out.printf("Then I shall call you Mrs. %s.", last);
}
else {System.out.printf("Then I shall call you Ms. %s.", last);}
}
else {System.out.printf("Then I shall call you %s %s", first, last);
}
}
}
}
Right now, when I run the code, I get an output such as this whenever I put the age over 20
What is your gender (M or F): M
First name: John
Last name: Smith
Age: 30
Are you married, John (y or n)?Then I shall call you Ms. Smith.
The program does not bother to prompt me for marriage status and directly assumes I'm an unmarried female. Can someone help me spot the error within my nested if statement? I've rewritten it 2 times already and am still unable to spot the problem.