public class Compound{
public static void main(String args[]){
int x = 20;
{ //复合语句1开始
int y = 40;
System.out.print(y);
int z = 245;
boolean b;
{ //复合语句2开始
b = y>z;
System.out.print(b);
} //复合语句2结束
} //复合语句1结束
String word = "hello java";
System.out.print(world);
}
}
public class Test {
public static void main(String args[]){
int x = 10;
if(x<20){
System.out.print("这是 if 语句");
}
}
}
2.if…else语句
public class Test {
public static void main(String args[]){
int x = 30;
if(x<20){
System.out.print("这是 if 语句");
}else{ //上面部分格式与if语句相同,这里else在两个大括号之间
System.out.print("这是 else 语句");
}
}
}
if…else语句
3.if…else if多分支语句
if…else if语句可以看成Python中的if…elif语句,这个作用是相同的
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{ //本语句可以与else一起使用
System.out.print("这是 else 语句");
}
}
}