Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

java - can you have two conditions in an if statement

I'm a beginner in coding. I was recently working with to create a chatting programme where a user will chat with my computer. Here is a part of the code:

System.out.println("Hello, what's our name? My name is " + answer4);
String a = scanner1.nextLine();
System.out.println("Ok, Hello, " + a + ", how was your day, good or bad?");
String b = scanner2.nextLine();

**if (b.equals("good"))** {                //1
    System.out.println("Thank goodness");
} else **if (b.equals("it was good"))** {  //2
    System.out.println("Thank goodness");
} else **if (b.equals("bad"))** {          //3
    System.out.println("Why was it bad?");
    String c = scanner3.nextLine();
    System.out.println("Don't worry, everything will be ok, ok?");
    String d= scanner10.nextLine();
} else **if (b.equals("it was bad"))**{    //4
    System.out.println("Why was it bad?");
    String c = scanner3.nextLine();
    System.out.println("Don't worry, everything will be ok, ok?");
    String d= scanner10.nextLine();
}

if(age<18){System.out.println("How was school?");}
else if (age>=18){System.out.println("How was work?");}

The conditions of the if statements are in Bold (surrounded with **). In case of first and the second condition I want my application to do same thing. Similarly third and fourth condition. I thought it was possible to somehow group them in if statement.

I tried with below code but it doesn't compile:

if (b.equals("good"), b.equals("it was good")) {
    System.out.println("Thank goodness");
}  else if (b.equals("bad"),(b.equals("it was bad"))) {
    System.out.println("Why was it bad?");
    String c = scanner3.nextLine();
    System.out.println("Don't worry, everything will be ok, ok?");
    String d= scanner10.nextLine();
}

Can someone correct it for me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use logical operators to combine your boolean expressions.

  • && is a logical and (both conditions need to be true)
  • || is a logical or (at least one condition needs to be true)
  • ^ is a xor (exactly one condition needs to be true)
  • (== compares objects by identity)

For example:

if (firstCondition && (secondCondition || thirdCondition)) {
    ...
}

There are also bitwise operators:

  • & is a bitwise and
  • | is a bitwise or
  • ^ is a xor

They are mainly used when operating with bits and bytes. However there is another difference, let's take again a look at this expression:

firstCondition && (secondCondition || thirdCondition)

If you use the logical operators and firstCondition evaluates to false then Java will not compute the second or third condition as the result of the whole logical expression is already known to be false. However if you use the bitwise operators then Java will not stop and continue computing everything:

firstCondition & (secondCondition | thirdCondition)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...