The
的
if-then
and
if-then-else
conditional statements let a
条件语句使
Java program make
Java程序可以对下一步的操作做出
simple decisions about what to do next. They work in the same logical way as we do when making
简单的决定 。 它们以与我们制作时相同的逻辑方式工作
For example, when making a plan with a friend, you could say "If Mike gets home before 5:00 PM, then we'll go out for an early dinner." When 5:00 PM arrives, the condition (i.e., Mike is home), which determines whether everyone goes out for an early dinner, will either be true or false. It works exactly the same in Java.
例如,当与朋友制定计划时,您可以说“如果迈克在下午5:00之前回家,那么我们将出去吃早饭。” 当5:00 PM到达时,确定每个人是否外出享用早饭的条件(即Mike在家)将为是或否。 它在Java中的工作原理完全相同。
if-then语句 ( The if-then Statement )
Let's say part of a program we're writing needs to calculate if the purchaser of a ticket is eligible for a child's discount. Anyone under the age of 16 gets a 10% discount on the ticket price.
假设我们正在编写的程序的一部分需要计算购票者是否有资格获得儿童折扣。 16岁以下的任何人都可以享受票价折扣10%。
We can let our program make this decision by using an
我们可以让我们的程序通过使用
if-then
if (age < 16)
isChild = true;
In our program, an integer variable called
boolean
variable
The syntax follows the same pattern every time. The
if (condition is true) execute this statement
The key thing to remember is the condition must equate to a
boolean
value
Often, a Java program needs to execute more than one statement if a condition is true. This is achieved by using a block (i.e., enclosing the statements in curly brackets):
如果条件为真,则Java程序通常需要执行多个语句。 这可以通过使用一个块来实现(即,将语句括在大括号中):
if (age < 16){
isChild = true;
discount = 10;}if (age < 16){
isChild = true;
discount = 10;}
This form of the
这种形式的
if-then
语句是最常用的语句,即使只有一条语句要执行,也建议使用大括号。 它提高了代码的可读性,并减少了编程错误。 如果没有大括号,则很容易忽略决策的效果,或者稍后再返回并添加另一个要执行的语句,却忘记了
if-then-else语句 ( The if-then-else Statement )
The
的
if-then
可以扩展为具有条件为false时执行的语句。 的
if-then-else
如果条件为真,则语句执行第一组语句,否则,执行第二组
if (condition)
{execute statement(s) if condition is true
}
else
{execute statement(s) if condition is false
}
In the ticket program, let's say we need to make sure the discount is equal to 0 if the ticket purchaser is not a child:
假设在购票程序中,如果购票者不是孩子,我们需要确保折扣等于0:
if (age < 16)
{
isChild = true;
discount = 10;
}
else
{
discount = 0;
}if (age < 16)
{
isChild = true;
discount = 10;
}
else
{
discount = 0;
}
The
的
if-then-else
语句还允许嵌套
if-then
陈述。 这允许决策遵循条件路径。 例如,票务程序可能有几个折扣。 我们可能先进行测试,看看购票者是否是孩子,然后如果他们是养老金领取者,那么他们是否
if (age < 16)
{
isChild = true;
discount = 10;
}
else if (age > 65)
{isPensioner = true; discount = 15;
}
else if (isStudent == true)
{
discount = 5;
}
As you can see, the
如您所见,
if-then-else
statement pattern just repeats itself. If at any time the condition is
true
, then the relevant statements are executed and any conditions beneath are not tested to see whether they are
true
or
For example, if the age of the ticket purchaser is 67, then the highlighted statements are executed and the
For example, if the age of the ticket purchaser is 67, then the highlighted statements are executed and the
There is something worth noting about the
有一点值得注意
(isStudent == true)
condition. The condition is written to make it clear that we're testing whether
isStudent
has a value of true, but because it is a
boolean
variable,
else if (isStudent)
{
discount = 5;
}
else if ( isStudent )
{
discount = 5;
}
If this is confusing, the way to think about it is like this -- we know a condition is tested to be true or false. For integer variables like
如果这令人困惑,那么思考的方式就是这样-我们知道条件被测试为是或否。 对于整数变量,例如
age
,我们必须编写一个可以计算为true或false的表达式(例如,
age == 12
,
age > 35<
However, boolean variables already evaluate to be true or false. We don't need to write an expression to prove it because
但是,布尔变量已经评估为true或false。 我们不需要编写表达式来证明它,因为
if (isStudent)
已经在说“如果isStudent为true ..”。 如果要测试布尔变量为false,只需使用一元运算符
!
。 它会反转一个布尔值,因此
if (!isStudent)
基本上是说“如果我
翻译自: https://www.thoughtco.com/the-if-then-and-if-then-else-statements-2033884