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

can you have two conditions in an if statement(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();
}

谁能帮我改正?

推荐答案

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

You can use logical operators to combine your boolean expressions.

  • &&是逻辑and(两个条件都需要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

主要用于bits and bytes操作.不过还有一点不同,我们再来看看这个表达式:

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 语句中可以有两个条件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:if 语句中可以有两个条件吗

基础教程推荐