JAVA_if或者怎么用,Java If语句

Java教程 - Java If语句

Java if语句用于根据条件执行一个代码块。

Java If语句

下面是Java if语句的最简单形式:if(condition)

statement;

condition是一个布尔表达式。如果condition是true那么执行语句。

如果condition是false,那么绕过语句。

以下代码根据an的值输出消息整数。 它使用if语句来执行检查。public class Main {

public static void main(String args[]) {

int num = 99;

if (num < 100) {

System.out.println("num is less than 100");

}

}

}

此程序生成的输出如下所示:

71b220d0c1b23c856107730af2cc65a1.png

例子

If语句经常用于比较两个变量。下面的代码定义了两个变量,x和y,它使用if语句来比较它们并打印出消息。public class Main {

public static void main(String args[]) {

int x, y;

x = 10;

y = 20;

if (x < y){

System.out.println("x is less than y");

}

x = x * 2;

if (x == y){

System.out.println("x now equal to y");

}

x = x * 2;

if (x > y){

System.out.println("x now greater than y");

}

if (x == y){

System.out.println("===");

}

}

}

此程序生成的输出如下所示:

e29c2f21d39063e5a3363beb07bed684.png

例2

我们还可以使用布尔值来控制if语句。boolean变量的值足以控制if语句。public class Main {

public static void main(String args[]) {

boolean b;

b = false;

if (b) {

System.out.println("This is executed.");

} else {

System.out.println("This is NOT executed.");

}

}

}

没有必要写如下的if语句:if(b == true) ...

此程序生成的输出如下所示:

4242d8646186cf8c23b0f47ebfdd9bb5.png

Java if else语句

if语句是条件分支语句。我们可以在if语句中添加else语句。

这里是if-else语句的一般形式:if (condition)

statement1;

else

statement2;

else子句是可选的。 每个语句可以是单个语句或复合语句用花括号括起来(一个块)。 只有一个语句可以直接出现在if或else之后。要包含更多语句,您需要创建一个块,如在这个片段中。

以下示例显示如何使用Java if else语句。public class Main {

public static void main(String[] argv) {

int i = 1;

if (i > 0) {

System.out.println("Here");

i -= 1;

} else

System.out.println("There");

}

}

]]>

输出:

3fac6648119d39e089c82ce2c6ba7751.png

在使用if语句时包含花括号是很好的,即使每个子句中只有一个语句。

Java if else梯形语句

if else梯形语句用于在多个条件下工作。

if-else-if梯形如下:if(condition)

statement;

else if(condition)

statement;

else if(condition)

statement;

.

.

else

statement;

这里是一个使用if-else-if梯形图的程序。public class Main {

public static void main(String args[]) {

int month = 4;

String value;

if (month == 1 )

value = "A";

else if (month == 2)

value = "B";

else if (month == 3)

value = "C";

else if (month == 4)

value = "D";

else

value = "Error";

System.out.println("value = " + value);

}

}

下面是程序产生的输出:

e3444f9bfab9ed6ddfae8bdb69506ca9.png

Java嵌套if语句

嵌套if是if语句在另一个if语句或else。

以下代码使用嵌套if语句来比较值。public class Main {

public static void main(String[] argv) {

int i = 10;

int j = 4;

int k = 200;

int a = 3;

int b = 5;

int c = 0;

int d =0;

if (i == 10) {

if (j < 20){

a = b;

}

if (k > 100){

c = d;

}

else{

a = c;

}

} else{

a = d;

}

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

System.out.println("d = " + d);

}

}

输出:

0cdcd1d73998e902fbf84c5be3fb6ca2.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值