01-顺序结构的流程图
02-单if语句的流程图
03-标准if-else语句的流程图
04-扩展if-else语句的流程图
05-for循环的流程图
06-while循环的流程图
/*
另一种循环控制语句是continue关键字。
一旦执行,立刻跳过当前次循环剩余内容,马上开始下一次循环。
*/
public class Demo15Continue {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 4) { // 如果当前是第4层
continue; // 那么跳过当前次循环,马上开始下一次(第5层)
}
System.out.println(i + "层到了。");
}
}
}
/*
三种循环的区别。
1. 如果条件判断从来没有满足过,那么for循环和while循环将会执行0次,但是do-while循环会执行至少一次。
2. for循环的变量在小括号当中定义,只有循环内部才可以使用。while循环和do-while循环初始化语句本来就在外面,所以出来循环之后还可以继续使用。
*/
public class Demo13LoopDifference {
public static void main(String[] args) {
for (int i = 1; i < 0; i++) {
System.out.println("Hello");
}
// System.out.println(i); // 这一行是错误写法!因为变量i定义在for循环小括号内,只有for循环自己才能用。
System.out.println("================");
int i = 1;
do {
System.out.println("World");
i++;
} while (i < 0);
// 现在已经超出了do-while循环的范围,我们仍然可以使用变量i
System.out.println(i); // 2
}
}
/*
do-while循环的标准格式:
do {
循环体
} while (条件判断);
扩展格式:
初始化语句
do {
循环体
步进语句
} while (条件判断);
*/
public class Demo11DoWhile {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("找到你啦!" + i);
}
System.out.println("===============");
int i = 1; // 1. 初始化语句
do {
System.out.println("找到你啦!" + i); // 3. 循环体
i++; // 4. 步进语句
} while (i <= 10); // 2. 条件判断
}
}
/*
switch语句使用的注意事项:
1. 多个case后面的数值不可以重复。
2. switch后面小括号当中只能是下列数据类型:
基本数据类型:byte/short/char/int
引用数据类型:String字符串、enum枚举
3. switch语句格式可以很灵活:前后顺序可以颠倒,而且break语句还可以省略。
“匹配哪一个case就从哪一个位置向下执行,直到遇到了break或者整体结束为止。”
*/
public class Demo08SwitchNotice {
public static void main(String[] args) {
int num = 2;
switch (num) {
case 1:
System.out.println("你好");
break;
case 2:
System.out.println("我好");
// break;
case 3:
System.out.println("大家好");
break;
default:
System.out.println("他好,我也好。");
break;
} // switch
}
}
public class Demo17LoopHourAndMinute {
public static void main(String[] args) {
for (int hour = 0; hour < 24; hour++) { // 外层控制小时
for (int minute = 0; minute < 60; minute++) { // 内层控制小时之内的分钟
System.out.println(hour + "点" + minute + "分");
}
}
}
}