Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello and good evening,

I've just started Java/programming two days ago and I seem to struggle with nested if/else statements. I've provided my current 'project' down below. The if statements when female works just fine, but the male statements always gives out both the possibility with Mr and the possibility with Name + lastName.

Thanks in advance! Please be respectful, I've really just started programming

Java
import java.util.Scanner;
public class GenderGame {

	public static void main(String[] args) {
		
		//System Objects
		Scanner keyboard = new Scanner(System.in);
		
		//Variables
		String Male = "Male";
		String Female = "Female";
		String Married;
		
		
		System.out.print("What is your gender (male or female):");
		String gender = keyboard.nextLine();
		System.out.print("Firstname:\t");
		String name = keyboard.nextLine();
		System.out.print("Last Name:\t");
		String lastName = keyboard.nextLine();
		System.out.print("Age:\t\t");
		int age = keyboard.nextInt();
		
		if (gender.equalsIgnoreCase(Female) && age >= 20) {
			System.out.print("Are you married, " + name + "? (yes or no)");
			Married = keyboard.next();
			if (Married.equalsIgnoreCase("yes"))
				System.out.print("Then I shall call you Mrs. " + lastName + ".");
			}else{
				System.out.print("Then I shall call you " + name + " " + lastName + ".");
			
	
		
		if (gender.equalsIgnoreCase(Male) && age >= 20) 
			System.out.print("Then I shall call you Mr. " + lastName + ".");
		if (gender.equalsIgnoreCase(Male) && age < 20) 
			System.out.print("Then I shall call you " + name + " " + lastName + ".");
		
	
		}
	}
}


What I have tried:

Tried a bunch of fiddeling around with the curly bracets but couldnt get it to work
Posted
Updated 7-Sep-18 1:23am
Comments
[no name] 12-Apr-17 18:55pm    
debugger

This sniffs of homework so I'm going to blurt out an answer, but look at your curly braces and make sure every closing brace is matched up with an opening brace and the other way around.
 
Share this answer
 
v2
Comments
Member 13124677 13-Apr-17 11:05am    
It ain't no homework. Im studying marketing, just learning programming for fun. But thanks anyway, i've managed to solve the problem :)
import java.util.Scanner;

public class GenderAndMarigeStatus {
// i made some changes and now is okey
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

String m = "Male";
String f = "Female";
String Married;

System.out.println("What is your gender (male or female):");
String gender = s.nextLine();

System.out.println("Firstname:");
String name = s.nextLine();
System.out.println("Last Name:");
String lastName = s.nextLine();
System.out.println("Age:");
int age = s.nextInt();

if (gender.equalsIgnoreCase("m") && age >= 20) {
System.out.println("Then I shall call you Mr. " + lastName + ".");
System.exit(0);
}
if (gender.equalsIgnoreCase("m") && age < 20) {
System.out.println("Then I shall call you " + name + " " + lastName + ".");
System.exit(0);
}
if (gender.equalsIgnoreCase(f) && age >= 20) {
}

System.out.println("Are you married, " + name + "? (yes or no)");
Married = s.next();

if (Married.equalsIgnoreCase("yes"))
System.out.println("Then I shall call you Mrs. " + lastName + ".");
else {
System.out.println("Then I shall call you " + name + " " + lastName + ".");

}
}
}
 
Share this answer
 
v2
Comments
Richard Deeming 7-Sep-18 9:58am    
An unformatted code-dump is not a solution to this already solved question.

And you don't help anyone by doing their homework for them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900