Java流程控制

// 选择结构
int score = 90;
if (score > 90) {
	System.out.println("通过");
} else {
	System.out.println("pass");
}

int score = 90;
if (score > 90) {
	System.out.println("通过");
} else if (score > 80) {
	System.out.println("考虑");
} else {
	System.out.println("pass");
}

// switch
switch 后面可以接受byte short char
byte/short/char -> int  这里发生了自动类型转换
switch (int or String) {
	case int or String:
}
switch (1) {
	case 1:
		System.out.println("星期一");
		break;
	case 2:
		System.out.println("星期二");
		break;
	default:
		System.out.println("其他");
}

// for
for (int i=0; i<= 10; i++) {
	System.out.println(i);
}
// 注:内层循环和外层的变量不能重名

// while
int i = 1;
while (i < 10){
	System.out.println(i);
	i++;
}

// do while
int i = 0;
do {
	System.out.println(i);
	i++;
}while(i < 10);

// break
for (int i=0; i<10; i++) {
	if (i == 5) {
		break;
	}
	System.out.println(i);
}

outter:for (int i=0; i<10; i++) {
	if (i == 5) {
		break outter;
	}
	System.out.println(i);
}

// continue
for (int i=0; i<10; i++) {
	if (i == 5) {
		continue;
	}
	System.out.println(i);
}
posted @ 2021-04-13 11:48  程序员陈师兄cxycsx  阅读(34)  评论(0编辑  收藏  举报