1顺序结构按顺序来
2 判断语句
if(关系表达式)){
语句体;
}
public class Demo02if {
public static void main(String[] args){
System.out.println("找到网吧");
int age = 16;
if (age >= 18){
System.out.println("HIGH!!");
}
System.out.println("回家吃饭");
}
}
2.2标准if语句
if(关系表达式){
语句体1;
}else{
语句体2;
}
2.3判断语句 if..else if..else
If(判断语句){
执行语句1;
}else if (判断语句2){
执行语句2;
}
.....
}else if(判断条件n){
执行语句n;
}else{
执行语句n+1;
}
public class Demo03if {
public static void main(String[] args){
int x = 3;
int y;
if (x >=3){
y = 2 * x + 1;
}else if (x >= -1 && x<3){
y = 2 * x;
}else {
y = 2 * x - 1;
}
System.out.println("y="+y);
}
}
三元换标准IF elseif
public class A3 {
public static void main(String[] args){
int a = 1;
int b = 2;
//三元运算:
int max = a > b ? a:b;
if语句
int max;
if(a > b){
max=a;
}esle{
max=b;
}
System.out.println("最大值:" + max)
}
}
switch 选择语句
switch(表达式){
case 常量1:
语句体1;
break;
case 常量2:
语句体2;
break;
...
default:
语句体n+1;
break;(只要符合上述要求,后面的不再执行)
}
switch语句使用的注意事项:
1 多个case后面的数据不可以重复
2swich后面的小空号党总只能是下列数据类型
基本数据类型:byte short char int
引用数据类型:String字符串、enum 枚举
3.swich语句格式可以很灵活。前后顺序可以点到。而且break语句还可以省略
匹配哪一个case就从那一个位置乡下执行,直到遇到break或者整体结束为止。
最后一个break可以省略!!!!!!!
省略中间的 下面的继续执行 因此可以出现多个println
本文详细讲解了Java中的基本控制结构,包括顺序结构、标准if语句、if...else、三元运算符和switch语句的用法示例,并强调了它们在程序逻辑中的关键作用。通过实例演示,读者将掌握如何在实际项目中灵活运用这些条件判断技术。

被折叠的 条评论
为什么被折叠?



