r/programmingbydoing • u/TeachMeSensei101 • Feb 15 '17
#37 Gender Game - Program Not Ending
I'm having trouble with the program ending. If I enter 'm below 20' the program doesn't output unless I write something else.
I can ask if they're f, married or not and the program will end. If they are below 20 the program won't end.
Can someone let me know where I went wrong and why the program will not end under these conditions?
Thanks!
My Code is below:
import java.util.Scanner;
public class GenderGame { public static void main( String[] args ) { Scanner keyboard = new Scanner(System.in);
String fname, lname, gender, married;
int age;
System.out.print( "What is your gender (m or f)" );
gender = keyboard.next();
System.out.println( "First Name:" );
fname = keyboard.next();
System.out.println( "Last Name:" );
lname = keyboard.next();
System.out.println( "age:" );
age = keyboard.nextInt();
//MR.
if ((gender.equals("m") && age >= 20))
{
System.out.println( "Then I shall call you Mr." + fname + lname );
}
else if ((gender.equals("m") && age < 20))
{
System.out.println( "Then I shall call you " + fname + lname );
}
//MRS.
if ((gender.equals("f") && age >= 20))
{
System.out.println( "Are you married?" );
}
married = keyboard.next();
if ((gender.equals("f") && age >= 20 && (married.equals("yes"))))
{
System.out.println("Then I shall call you Mr.s " + fname + lname);
}
else if ((gender.equals("f") && age >= 20 && (married.equals("no"))))
{
System.out.println("Then I shall call you Ms. " + fname + lname);
}
//MS.
if ((gender.equals("f") && age < 20))
{
System.out.println( "Then I shall call you " + fname + lname );
}
}
}
2
Upvotes
View all comments
Show parent comment
2
u/holyteach Feb 15 '17
The if block that you moved it to is the one he was talking about.
Your input statement was below that block, but it needed to be inside the block instead.