- My Forums
- Tiger Rant
- LSU Recruiting
- SEC Rant
- Saints Talk
- Pelicans Talk
- More Sports Board
- Fantasy Sports
- Golf Board
- Soccer Board
- O-T Lounge
- Tech Board
- Home/Garden Board
- Outdoor Board
- Health/Fitness Board
- Movie/TV Board
- Book Board
- Music Board
- Political Talk
- Money Talk
- Fark Board
- Gaming Board
- Travel Board
- Food/Drink Board
- Ticket Exchange
- TD Help Board
Customize My Forums- View All Forums
- Show Left Links
- Topic Sort Options
- Trending Topics
- Recent Topics
- Active Topics
Started By
Message
Help me with my nested if statements in java (self-learning Novice)
Posted on 12/14/13 at 8:13 am
Posted on 12/14/13 at 8:13 am
I am having trouble with the following code that is supposed to ask questions about gender and age, and if over 20, ask if the user is married. If they a woman, 20+, and married then I title them "Mrs.", if woman, not married, but 20+, "Ms.", if man of 20+ years, and married or not, "Mr.", and if any user is under 20: just their name is put back without any title.
I feel like I've tried many ways to get this to work. I am trying to use nested ifs and AND in this code. The operations should make sense with the way I've structured them yet, if someone is 20+ and married, they always come out as a Mr.
Any help would be appreciated. TIA
The code.
I feel like I've tried many ways to get this to work. I am trying to use nested ifs and AND in this code. The operations should make sense with the way I've structured them yet, if someone is 20+ and married, they always come out as a Mr.
Any help would be appreciated. TIA
The code.
quote:
import java.util.Scanner;
public class GenderGame
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String gender, first_name, last_name, married;
int age;
System.out.println( "What is your gender? (M or F):" );
gender = keyboard.next();
System.out.println( "First name: " );
first_name = keyboard.next();
System.out.println( "Last name: " );
last_name = keyboard.next();
System.out.println( "Age: ");
age = keyboard.nextInt();
String male, female, yes, no;
male = "M";
female = "F";
yes = "Y";
no = "N";
if( age >= 20 ) {
System.out.println( "Are you married, " + first_name + "? (Y or N) " );
married = keyboard.next();
if( gender == female && married == yes ) {
System.out.println( "Then you are called Mrs. " + first_name + " " + last_name + "." );
}
else if( gender == female && married == no ) {
System.out.println( "Then you are called Ms. " + first_name + " " + last_name + "." );
}
else {
System.out.println( "Then you are called Mr. " + first_name + " " + last_name + "." );
}
}
else {
System.out.println( "Then you are called " + first_name + " " + last_name + "." );
}
}
}
Posted on 12/14/13 at 8:42 am to Pectus
Shouldn't your if statements be looking for married = Y or N instead of yes or no?
Posted on 12/14/13 at 9:08 am to tom
I have an input of "Y" = to the variable yes to compare it to other variables later.
But if that's the issue, then it explains why I only get Mr. (due to the else).
But if that's the issue, then it explains why I only get Mr. (due to the else).
This post was edited on 12/14/13 at 9:10 am
Posted on 12/14/13 at 9:09 am to Pectus
If you're going to be doing a lot more of this then I recommend this site. Stackoverflow
A lot of programmers go there when they get stuck.
A lot of programmers go there when they get stuck.
Posted on 12/14/13 at 9:55 am to Pectus
What you can do in situations like this is, right before the:
if(gender == female && married = yes)
block, print out the values of gender, female, married, and yes. This will let you see, from the program's perspective, what those values are and make sure they match up.
If they match up, it may be an issue of comparing strings. I'm not sure if java has different functions for strings like this or not, but I know C had strcmp() for comparing them.
Just by looking at it, I have a feeling the program is seeing a difference between declaring a variable as a string and reading one in from keyboard.next()
if(gender == female && married = yes)
block, print out the values of gender, female, married, and yes. This will let you see, from the program's perspective, what those values are and make sure they match up.
If they match up, it may be an issue of comparing strings. I'm not sure if java has different functions for strings like this or not, but I know C had strcmp() for comparing them.
Just by looking at it, I have a feeling the program is seeing a difference between declaring a variable as a string and reading one in from keyboard.next()
This post was edited on 12/14/13 at 10:00 am
Popular
Back to top
Follow TigerDroppings for LSU Football News