java的流程控制
1、if-else
有三种
if(条件表达式){
执行语句
}
2.if (条件表达式){
执行语句
}else {
执行语句
}
3.if (条件表达式){
执行语句1
}else if (条件表达式){
执行语句2
}else if (条件表达式){
执行语句3
}else { --------------------------else可以不用加
执行语句4
}*/
2.switch-case
这里的表达式只能是以下的6种数据类型:byte、short、char、int、枚举、String(jdk7新增)
break是跳出结构的,如果不加则会继续执行
default相当于if-else中的else
switch(表达式){
case 常量1:
执行语句1;
break;
case 常量2:
执行语句2;
break;
case 常量3:
执行语句3;
break;
case 常量4:
执行语句4;
break;
default:
执行语句n;
}
String season = "summer";
switch (season){
case "spring":
System.out.println("春天");
break;
case "summer":
System.out.println("夏天");
break;
case "autumn":
System.out.println("秋天");
break;
case "winter":
System.out.println("冬天");
break;
default:
System.out.println("输入有误");
}
特例:判断成绩,大于60及格,小于60不及格

3.for循环

例:
输出水仙花数
是一个三位数,各个位的立方和等于本身
153 = 1*1*1 + 3*3*3 + 5*5*5
for (int i = 100; i <= 999; i++) {
int bai = i / 100;
int shi = i % 100 / 10;
int ge = i % 10;
if (bai*bai*bai + shi*shi*shi + ge*ge*ge == i){
System.out.println(i);
}
}
4.while
while(条件表达式){
}
注意while的迭代条件,避免出现死循环
5.do while
至少会执行一次循环体
do{
}while()
浙公网安备 33010602011771号