梦入琼楼寒有月,行过石树冻无烟

JAVA 条件语句

if…else

1
2
3
4
5
if ( i == 10) {
// 满足条件执行
} else {
// 不满足条件执行
}

​ 实例

1
2
3
4
5
6
7
8
9
10
11
public class Demo {
public static void main (String args[]) {
int i = 10;

if (i == 10) {
System.out.println("满足条件");
} else {
System.out.println("不满足条件");
}
}
}

if…else if…else

1
2
3
4
5
6
7
if (i == 1) {
// 如果满足 1 执行
} else if (i ==2) {
// 如果满足 2 执行
} else {
// 如果都不满足执行
}

​ 实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Demo {
public static void main (String args[]) {
int i = 10;

if (i == 1) {
System.out.println("=1 ");
} else if (i == 2) {
System.out.println("=2 ");
} else if (i == 3) {
System.out.println("=3 ");
} else if (i == 4) {
System.out.println("=4 ");
} else if (i == 5) {
System.out.println("=5 ");
} else {
System.out.println("NO ");
}
}
}

if .. if

1
2
3
4
5
6
if (i == 10) {
if (n == 20) {
if (t == 30) {
}
}
}

​ 实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Demo {
public static void main (String args[]) {
int i = 10;
int n = 20;
int t = 30;

if (i == 10) {
System.out.println("=10");
if (n == 20) {
System.out.println("=20");
if (t == 10) {
System.out.println("=30");
} else {
System.out.println("No");
}
} else {
System.out.println("NO");
}
} else {
System.out.println("NO");
}
}
}

if .. if .. else ..else

1
2
3
4
5
6
7
8
9
10
11
12
13
if (i == 10) {
if (n == 20) {
if (t == 30) {

} else {
// 30
}
} else {
// 20
}
} else {
// 10
}

if .. if .. else if else ..if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (i == 10) {
// 10
if (n == 20) {
// 20
} else if (n == 10) {
// 20
} else {
// 20
}
} else if (i == 2) {
// 10
} else {
// 10
}

​ 实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (i == 10) {
// 10
if (n == 20) {
// 20
if (t == 30) {
// 30
} else if (t == 10) {
// 30
} else {
// 30
}
} else if (n == 20) {
// 20
} else {
// 20
}
} else if (i == 10) {
// 10
} else {
// 10
}
⬅️ Go back