can you have two conditions in an if statement(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 betrue
)||
is a logical or (at least one condition needs to betrue
)^
is a xor (exactly one condition needs to betrue
)- (
==
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 语句中可以有两个条件吗
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01