java中的条件语句_Java中的条件语句

java中的条件语句

Conditional statements in a computer program support decisions based on a certain condition. If the condition is met, or "true," a certain piece of code is executed.

计算机程序中的条件语句支持基于特定条件的决策 。 如果满足条件或为“ true”,则执行一段代码。

For example, you want to convert user-entered text to lowercase. Execute the code only if the user entered capitalized text. If not, you don't want to execute the code because it will lead to a runtime error.

例如,您要将用户输入的文本转换为小写。 仅当用户输入大写字母时才执行代码。 如果没有,您就不想执行代码,因为它会导致运行时错误。

There are two main conditional statements used in Java: the if-then and if-then-else statements, and the switch statement.

Java中使用了两个主要的条件语句:if-then和if-then-else语句,以及switch 声明。

If-Then和If-Then-Else语句 ( The If-Then and If-Then-Else Statements )

The most basic flow control statement in Java is if-then: if [something] is true, do [something]. This statement is a good choice for simple decisions. The basic structure of an if statement starts with the word "if," followed by the statement to test, followed by curly braces that wrap the action to take if the statement is true. It looks like this:

Java中最基本的流控制语句是if-then:如果[something]为true,则执行[something]。 该语句是简单决策的不错选择。 if语句的基本结构以单词“ if”开头,其后是要测试的语句,然后是大括号,该大括号包装了在语句为true时要采取的操作。 看起来像这样:

if ( statement ) {// do something here....}

if(statement){//在这里做某事。...}

This statement can also be extended to do something else if the condition is false:

该语句也可以扩展为做其他事情 如果条件为假:

if ( statement ) { // do something here...}else {// do something else...}

if(statement){//在这里做某事...}其他{//做其他事...}

For example, if you are determining whether someone is old enough to drive, you might have a statement that says "if your age is 16 or older, you can drive; else, you cannot drive."

例如,如果要确定某人是否年龄足够大,可以开车,那么您可能会说:“如果您的年龄为16岁或以上,您可以开车;否则,您不能开车。”

int age = 17;if age >= 16 {System.out.println("You can drive.");}else {System.out.println("You are not old enough to drive.")

int age = 17;如果age> = 16 {System.out.println(“您可以开车。”);} else {System.out.println(“您年龄还不够开车。”)

There is no limit to the number of else statements you can add. 

您可以添加的else语句的数量没有限制。

条件运算符 ( Conditional Operators )

In the example above, we used a single operator. These are the standard operators you can use:

在上面的示例中,我们使用了一个运算符。 这些是您可以使用的标准运算符:

  • equal to: =

    等于:=
  • less than: <

    小于:
  • more than: >

    超过:
  • greater than or equal to: >=

    大于或等于:> =
  • less than or equal to: >=

    小于或等于:> =

In addition to these, there are four more operators used with conditional statements:

除了这些,还有四个与条件语句一起使用的运算符

  • and: &&

    和:&&
  • not:! 

    不:!
  • or: ||

    或:||
  • is equal to: == 

    等于:==

For example, the driving age is considered to be from age 16 to age 85, in which case the AND operator can be used.

例如,驾驶年龄被认为是16岁至85岁,在这种情况下可以使用AND运算符。

else if ( age > 16 && age < 85 )

否则(年龄> 16岁&&年龄<85岁)

This will return true only if both conditions are met. The operators NOT, OR, and IS EQUAL TO can be used in a similar way.

仅当同时满足两个条件时,它才会返回true。 运算符NOT,OR和IS EQUAL TO可以类似的方式使用。

切换语句 ( The Switch Statement )

The switch statement provides an effective way to deal with a section of code that could branch in multiple directions based on a single variable. It does not support the conditional operators the if-then statement does, nor can it handle multiple variables. It is, however, a preferable choice when the condition will be met by a single variable because it can improve performance and is easier to maintain.

开关 语句提供了一种有效的方式来处理一段代码,该代码段可以基于单个代码在多个方向上分支 变量。 它不支持if-then语句支持的条件运算符,也不能处理多个变量。 但是,当通过单个变量满足条件时,这是一个较好的选择,因为它可以提高性能并且更易于维护。

 Here's an example:

这是一个例子:

switch ( single_variable ) {case value://code_here;break;case value://code_here;break;default://set a default;}

开关(single_variable){case value:// code_here; break; case value:// code_here; break; default://设置默认值;}

Note that you start with the switch, provide a single variable and then set out your choices using the term case. The keyword break completes each case of the switch statement. The default value is optional, but good practice.

请注意,从开关开始,提供一个变量,然后使用术语case列出您的选择。 关键字break可以完成switch语句的每种情况。 默认值是可选的,但是是很好的做法。

For example, this switch prints the lyric of the song Twelve Days of Christmas given a provided day.

例如,此开关在给定的日期打印圣诞节十二天的歌曲的歌词。

int day = 5;

诠释日= 5;

String lyric = ""; // empty string to hold the lyric

字符串歌词=“”; //空字符串以容纳歌词

switch (day) {case 1:

切换(天){情况1:

lyric = "A partridge in a pear tree.";break;case 2:lyric = "2 turtle doves";break;case 3:lyric = "3 French hens";break;case 4:lyric = "4 calling birds";break;case 5:lyric = "5 gold rings";break;case 6:lyric = "6 geese-a-laying";break;case 7:lyric = "7 swans-a-swimming";break;case 8:lyric = "8 maids-a-milking";break;case 9:lyric = "9 ladies dancing";break;case 10:lyric = "10 Lords-a-leaping";break;case 11:lyric = "11 pipers piping";break;case 12:lyric = "12 drummers drumming";break;default:lyric = "There are only 12 days.";break;}System.out.println(lyric);

;抒情=“梨树上的part。”;休息;案例2:抒情=“ 2只乌龟鸽子”;休息;案例3:抒情=“ 3只法国母鸡”;休息;案例4:抒情=“ 4只主叫鸟” ; break;案例5:抒情诗=“ 5金戒指”; break;案例6:抒情诗=“ 6只鹅躺着”; break;案例7:抒情诗=“ 7个天鹅游泳”; break;案例8 :lyric =“ 8个挤奶的女仆”;休息;情况9:抒情=“ 9跳舞的女士”;休息;情况10:抒情的=“ 10个上议院的游”“;休息;情况11:歌词=” 11 piper pipe“; break; case 12:lyric =” 12个鼓手打鼓“; break;默认值:lyric =”只有12天。“; break;} System.out.println(lyric);

In this example, the value to test is an integer. Java SE 7 and later support a string object in the expression. For example:String day = "second";String lyric = ""; // empty string to hold the lyric

在此示例中,要测试的值为整数。 Java SE 7和更高版本在表达式中支持字符串对象。 例如:String day =“ second”; String lyric =“”; //空字符串以容纳歌词

switch (day) {case "first":lyric = "A partridge in a pear tree.";break;case "second":lyric = "2 turtle doves";break;case "third":lyric = "3 French hens";break;// etc. 

开关(天){case“ first”:lyric =“梨树上的part。”; break; case“ second”:lyric =“ 2只乌龟鸽子”; break; case“ third”:lyric =“ 3只法国母鸡“; break; //等。

翻译自: https://www.thoughtco.com/conditional-statements-2034048

java中的条件语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值