java流程控制
一、if语句判断
int score = 87;
if(score>90) {
System.out.println("优");
}
else if (score>80 && score<=90) {
System.out.println("良好");
}
else if (score >60 && score <=80) {
System.out.println("及格");
}
else {
System.out.println("不及格");
}
二、switch循环
int score = 8;
switch (score) {
case 9:System.out.println("优秀!");break;
case 8:System.out.println("良好!");break;
case 6:System.out.println("及格!");break;
case 5:System.out.println("待拯救!");break;
case 4:System.out.println("不及格!");break;
case 3:System.out.println("不及格!");break;
case 2:System.out.println("不及格!");break;
case 1:System.out.println("不及格!");break;
case 0:System.out.println("不及格!");break;
default:System.out.println("无效");break;
}
三、do…while循环
do {
summ = summ +j;
// System.out.println("summ="+ summ + " j=" + j);
j++;
} while (j<=100);
或者
do {
System.out.println("请输入密码:");
pwd1 = sc.nextLine();
System.out.println("请再次输入密码:");
pwd2 = sc.nextLine();
if (!pwd1.equals(pwd2)) {
System.out.println("输入的密码不一致,重新输入!");
}
}while(!pwd1.equals(pwd2));
四、for循环、foreach循环
for (int j = 1; j <=100; j++) {
sum = sum + j;
}
int arr[] = {7,10,1};
for(int x:arr) {
System.out.println("foreach语句遍历arr并赋给x的值为:"+x);
}
五、嵌套与标签控制
循环中遇到break直接中断循环
int y = 1;
while (y>0) {
y++;
System.out.println(y);
if (y == 10) {
break;
}
}
循环中遇到continue将跳过当前值
for(int x=1;x<=10;x++) {
if(x % 2 ==0) {
continue;
}
System.out.println(x);
}
带标签的循环与嵌套
/* out:for(int x=0;x<=3;x++) {
System.out.println("x="+x);
for(int y=0;y<=6;y++) {
System.out.println("y="+y);
if(y == 4) {
break out; //采用标签标记的方式使外层循环在y = 4时暂停
}
// if(y == 4) {
// break;//这里break控制的是内层循环
// }
}
}*/
说总是让人不省心,试一下。