1.程序逻辑控制
- 顺序结构
1.1程序从上到下逐行执行,一直到程序末尾
2选择(分支)结构:if,switch。2.1:If,if…else,if…eles if…eles,
1.1: if(布尔值表达式){ 1.2: if(布尔值表达式){
条件满足时执行的程序 条件满足时执行的程序;
} } else{
1.3:if(布尔值表达式){ 条件不满足时执行的程序;
条件满足时执行的程序; }
} else if(布尔值表达式2){
条件满足时执行的程序;
} else {
条件不满足时执行的程序;
}
2.2 :使用switch结构:
\if可以判断布尔表达式,而switch只能够判断内容(支持String 的判断)\、
break跳出switch结构,若无break则一直往下执行\
int ch = 1;
switch (ch){ //判断内容是否是数字
case 1:{ // 判断内容是否是1
System.out.println("内容是1…..");
break;
} case 2: { // 判断内容是否是
System.out.println("内容是2….);
break;
} case 3:{
System.out.println("内容是3….);
//continue语句只是结束本次循环,而不终止整个循环的执行;而break语句则是强制终止整个循环过程。
}
default:{ //判断内容都不满足
System.out.println("没有匹配的内容");
} }
While循环和 for循环
3.1 whlie循环和do….while
int i = 1;
while ( i <= 100 //循环判断 )
{System.out.println(“我爱你! ");//循环语句;
continue; //跳出本次循
i ++; //修改循环结束条件;
}
3.2 do {//先循环一次
System.out.println("唱歌 !");
System.out.println("演奏肖邦钢琴曲 !");
System.out.print("可以了吗 ?(y/n):");
answer = input.next(); //输入语句
} //先循环一次
3.3for循环
For(循环条件初始化;循环判断;循环变更条件){
循环语句;
}
int i = 0, j = 0;
int x= 8;
for(i = 1;i <= x;i++){
for(j = 0;j < (hang + i - 1);j++){[1]
System.out.print (" ");
}else{//[2]
System.out.print ("*");
} }
System.out.println ();
} } }