if 判断
1
2
3
4
5
6
7
8
9
10
11
|
public class HelloWorld {
public static void main(String[] args) {
boolean b = true ;
//如果成立就打印yes
if (b){
System.out.println( "yes" );
}
}
}
|
多表达式与一个表达式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public class HelloWorld {
public static void main(String[] args) {
boolean b = false ;
//如果有多个表达式,必须用大括弧包括起来
if (b){
System.out.println( "yes1" );
System.out.println( "yes2" );
System.out.println( "yes3" );
}
//否则表达式2 3 无论b是否为true都会执行
if (b)
System.out.println( "yes1" );
System.out.println( "yes2" );
System.out.println( "yes3" );
//如果只有一个表达式可以不用写括弧,看上去会简约一些
if (b){
System.out.println( "yes1" );
}
if (b)
System.out.println( "yes1" );
}
}
|
if 使用过程中可能遇到的坑
在第6行,if后面有一个分号; 而分号也是一个完整的表达式
如果b为true,会执行这个分号,然后打印yes
如果b为false,不会执行这个分号,然后打印yes
这样,看上去无论如何都会打印yes
1
2
3
4
5
6
7
8
9
10
|
public class HelloWorld {
public static void main(String[] args) {
boolean b = false ;
if (b);
System.out.println( "yes" );
}
}
|
if else
else 代表不成立的情况

else if
else if 是多条件判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class HelloWorld {
public static void main(String[] args) {
//如果只使用 if,会执行4次判断
int i = 2 ;
if (i== 1 )
System.out.println( 1 );
if (i== 2 )
System.out.println( 2 );
if (i== 3 )
System.out.println( 3 );
if (i== 4 )
System.out.println( 4 );
//如果使用else if, 一旦在18行,判断成立, 20行和22行的判断就不会执行了,节约了运算资源
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 );
}
}
|
switch
switch可以使用byte,short,int,char,String,enum
注: 每个表达式结束,都应该有一个break;
注: String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数
注: enum是枚举类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
public class HelloWorld {
public static void main(String[] args) {
//如果使用if else
int day = 5 ;
if (day== 1 )
System.out.println( "星期一" );
else if (day== 2 )
System.out.println( "星期二" );
else if (day== 3 )
System.out.println( "星期三" );
else if (day== 4 )
System.out.println( "星期四" );
else if (day== 5 )
System.out.println( "星期五" );
else if (day== 6 )
System.out.println( "星期六" );
else if (day== 7 )
System.out.println( "星期天" );
else
System.out.println( "这个是什么鬼?" );
//如果使用switch
switch (day){
case 1 :
System.out.println( "星期一" );
break ;
case 2 :
System.out.println( "星期二" );
break ;
case 3 :
System.out.println( "星期三" );
break ;
case 4 :
System.out.println( "星期四" );
break ;
case 5 :
System.out.println( "星期五" );
break ;
case 6 :
System.out.println( "星期六" );
break ;
case 7 :
System.out.println( "星期天" );
break ;
default :
System.out.println( "这个是什么鬼?" );
}
}
}
|
while条件为true时 重复执行
只要while中的表达式成立,就会不断地循环执行
1
2
3
4
5
6
7
8
9
10
11
|
public class HelloWorld {
public static void main(String[] args) {
//打印0到4
int i = 0 ;
while (i< 5 ){
System.out.println(i);
i++;
}
}
}
|
While条件为true时 重复执行,至少会执行一次
do{
} while 循环
与while的区别是,无论是否成立,先执行一次,再进行判断
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class HelloWorld {
public static void main(String[] args) {
//打印0到4
//与while的区别是,无论是否成立,先执行一次,再进行判断
int i = 0 ;
do {
System.out.println(i);
i++;
} while (i< 5 );
}
}
|