结构图
代码从上往下逐行执行
选择结构语法规则:
if(条件表达式){
//……
}else{
//……
}
if(条件表达式){
//……
}else if(条件表达式){
//……
} else if(条件表达式){
//……
}…..
else{
//……
}
示例:
public static void main(String[] args) {
int a =1;
if (a>3) {
System.out.println("大");
}
if (a>3) {
System.out.println("大于");
}else {
System.out.println("小于");
}
final int score = 18;
if (score>90) {
System.out.println("优秀");
}else if(score>70){
System.out.println("良好");
}else if(score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
分支结构
语法规则:
switch(条件表达式){
case 条件1:
//……
break;
case 条件2:
//……
break;
……
default:
//……
}
示例:
public static void main(String[] args) {
final String jiJie = "SPRING";
switch (jiJie){
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("输入错误");
}
}
示例:
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");
break;
case 6:
case 7:
System.out.println("休息日");
break;
default:
System.out.println("输入错误");
}
}
For循环
语法:
for(数据类型 循环变量;循环继续条件;循环变量自动修正){
//……
}
示例:
public static void main(String[] args) {
for(int i =0;i
关注
打赏