java分支结构

if else

IfElse.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test {

public static void main(String args[]){
int x = 30;

if( x == 10 ){
System.out.print("Value of X is 10");
}
else if( x == 20 ){
System.out.print("Value of X is 20");
}
else if( x == 30 ){
System.out.print("Value of X is 30");
}
else{
System.out.print("这是 else 语句");
}
}
}

嵌套的if…else语句

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test {

public static void main(String args[]){
int x = 30;
int y = 10;

if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
}

switch语句

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 Test {

public static void main(String args[]){
//char grade = args[0].charAt(0);
char grade = 'C';

switch(grade)
{
case 'A' :
System.out.println("优秀");
break;
case 'B' :
case 'C' :
System.out.println("良好");
break;
case 'D' :
System.out.println("及格");
case 'F' :
System.out.println("你需要继续努力");
break;
default :
System.out.println("无效等级");
}
System.out.println("你的等级是 " + grade);
}
}