Java if statement strings and more

I decided to try learning a bit in java tonight, and I was just trying to try something with the things that I learned.

My question is if statement, how to make two bites true. Here is what I still have.

if ("male".equals(gender)) && ("brendan".equals(name)) 

The problem, I'm sure, is & & but I'm not sure.

EDIT EDIT EDIT.

This is a fixed part of the problem.
if("male".equals(gender) && "Brendan".equals(name))
and so it happened

 if (("male".equals(gender)) && ("brendan".equals(name))) 

Now I will post everything that I have another problem, and you probably will need to see all this.

 import java.util.Scanner; public class my_input_and_if_statements { public static void main (String args[]) { Scanner jonks = new Scanner(System.in); String name, gender, answer; System.out.println("What is your name?"); name = jonks.next(); System.out.print("Hello "); System.out.print( name); System.out.println(" are you male or female?"); gender = jonks.next(); if (("male".equals(gender)) && ("brendan".equals(name))) { System.out.println("blah blah"); } else { System.out.println(" Welcome to one of my first java applications. I hope you like it"); } if (("female".equals(gender)) && ("nicole".equals(name))) { System.out.println("blah blah 2"); } else { System.out.println(" Welcome to one of my first java applications. I hope you like it"); } } } 

Sorry if this seems pointless, just trying to tie some things together before I start trying to find out something else.

now when i go, it gives me two lines when it ends, or it gives me both.

+4
source share
4 answers

I think these are your parentheses.

The if statement has the form if(condition) , where condition is some Boolean expression. In this case, you want the condition to be "male".equals(gender) && "Brendan".equals(name) , so your complete statement should look something like this:

 if("male".equals(gender) && "Brendan".equals(name)) { //Foo Blah blah } else { //Barr fuzz fuzz } 

For the second part of your question, you have two if , both of which are printed on both the if and the else branches, so you are going to hit one or the other of println in the first if and one or the other of the println if second if . I think what you probably wanted to do is something like the following:

 if(gender.equals("male") && name.equals("Brendan")) { println("Brendan Message"); } else if(gender.equals("female") && name.equals("Nicole")) { println("Nicole Message"); } else { println("Anybody Message"); } 
+1
source
 if (("male".equals(gender)) && ("brendan".equals(name))) ^ ^ 

if(condition) enclosed brackets are needed.

For your many comparisons, you can have a man or woman, not both, so instead you can use the nested if..else construct,

Otherwise, you will perform a male / female assessment for each name unnecessarily. The following looks more natural to me.


 if ("male".equals(gender)){ if("brendan".equals(name)){ System.out.println("right"); }else{ System.out.println("wrong wrong"); } //add more cases here }else{ //definitely female } 
+1
source

One missing ( and extra parentheses.

 if ("male".equals(gender) && "brendan".equals(name)) 
+1
source

I think he wants to know about transfers. as

  public enum Gender { MALE = "Male"; FEMALE = "Female"; } 

or the best way to create an enumeration of strings?

0
source

Source: https://habr.com/ru/post/1445739/


All Articles