你可以在if语句中有两个条件吗

你可以在if语句中有两个条件吗

本文介绍了你可以在if语句中有两个条件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码的初学者。最近,我正在创建一个聊天程序,用户可以在其中与我的计算机聊天。这是代码的一部分:

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?");}

if语句的条件位于粗体(用 ** 包围)。在第一种情况和第二种情况下,我希望我的应用程序执行相同的操作。同样的第三和第四条件。我认为可以以某种方式将它们分组在 if 语句中。

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();
}

有人可以为我更正吗?

推荐答案

您可以使用逻辑运算符组合您的布尔表达式


  • & 是合乎逻辑的(两个条件都必须为 true

  • || 是逻辑上的(至少一个条件必须为 true

  • ^ xor (恰好一个条件需要为 true

  • == 通过身份比较对象)

  • && 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)

例如:

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

还有按位运算符:


  • & 是按位

  • | 是按位

  • ^ xor

  • & 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)

如果使用逻辑运算符和 firstCondition 的计算结果为 false ,然后 Java 不会计算第二个或整个逻辑表达式的结果的第三个条件已知为 false 。但是,如果使用按位运算符,则 Java 不会停止并继续计算所有内容:

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)

这篇关于你可以在if语句中有两个条件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:13