概念:流程是指程序步骤执行的先后顺序
分类:
- 顺序结构
- 选择结构
- 循环结构
顺序结构
public static void main(String[] args) {
System.out.println("11111");
System.out.println("22222");
System.out.println("33333");
System.out.println("44444");
}
选择结构
- if语句
if (布尔表达式) {
语句体
}
- 标准的if-else语句
if (布尔表达式) {
语句体A
} else {
语句体B
}
- 扩展的if-else语句
if (条件判断1) {
语句体1
} else if (条件判断2) {
语句体2
}
...
else if (条件判断N) {
语句体N
} else {
语句体N+1
}
public static void main(String[] args) {
int age = 19;
if (age >= 18) {
System.out.println("成年");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入考试成绩:");
int score = sc.nextInt();
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("良好");
} else if (score >= 70 && score < 80) {
System.out.println("中等");
} else if (score >= 60 && score < 70) {
System.out.println("及格");
} else if (score >= 0 && score < 60) {
System.out.println("不及格");
} else {
System.out.println("数据错误");
}
}
循环结构
- for循环
for (1.初始化语句; 2.条件判断; 4.步进语句) {
3.循环体;
}
public static void main(String[] args) {
for (int i = 0; i <= 732; i++) {
System.out.println("i = " + i);
}
}
- while循环
- 标准格式
while (1.条件判断) {
2.循环体
}
- 扩展格式
1.初始化语句
while (2.条件判断) {
3.循环体
4.步进语句
}
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
System.out.println("i = " + i);
i++;
}
}
跳转控制语句
循环当中可以使用两种跳转控制语句:
- break语句
- continue语句
break语句
一旦执行这个语句,整个循环立刻结束
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 4) {
break;
}
System.out.println("i = " + i);
}
}
continue语句
一旦执行这个语句,当前次循环剩余内容立刻跳过,马上开始下一次循环
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 4) {
continue;
}
System.out.println("i = " + i);
}
}
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i); //这个输出语句就是循环体剩余内容
}
System.out.println("======================");
int i = 1;
while (i <= 10) {
if (i == 4) {
System.out.println("即将执行continue语句");
continue;
}
System.out.println(i); //这一行就是循环体剩余内容
i++; //步进表达式,这也是循环体剩余内容,也会被continue跳过
}
}
死循环
标准格式:
while (true) {
循环体
}
等效的格式:
for (;;) {
循环体
}
public static void main(String[] args) {
// while (true) {
// System.out.println("a");
// }
for (;;) {
System.out.println("a");
}
}
循环嵌套中的控制语句
- break和continue只能默认作用于所属外层的循环
- 如果希望写在内层,但是作用于外层循环
标签名称: for (...) {
for (...) {
Break 标签名称;
}
}
标签名称:一个自定义的标识符
public static void main(String[] args) {
int count = 0;
label: for (int i = 1; i <= 10; i++) {
// if (i == 3) {
// continue;
// }
for (int j = 1; j <= 5; j++) {
// if (j == 3) {
// continue;
// }
if (j == 3) {
break label;
}
count++;
}
}
System.out.println("总人数:" + count);
}
code
import java.util.Scanner;
public class MaxTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = sc.nextInt();
System.out.println("请输入第二个数字:");
int b = sc.nextInt();
int max;
if (a > b) {
max = a;
} else {
max = b;
}
System.out.println("最大值:" + max);
}
}
------------------------------------------------------------------------------------------
import java.util.Scanner;
public class MaxThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = sc.nextInt();
System.out.println("请输入第二个数字:");
int b = sc.nextInt();
System.out.println("请输入第三个数字:");
int c = sc.nextInt();
int temp;
if (a > b) {
temp = a;
} else {
temp = b;
}
int max;
if (temp > c) {
max = temp;
} else {
max = c;
}
System.out.println("最大值:" + max);
}
}
--------------------------------------------------------------------------------------------------
public class ForSum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println("偶数和:" + sum);
}
}
----------------------------------------------------------------------------------------------------
public class WhileSum {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
if (i % 2 == 1) {
sum += i;
}
i++;
}
System.out.println("奇数和:" + sum);
}
}
------------------------------------------------------------------------------------------------
public class ForVsWhile {
public static void main(String[] args) {
for (int a = 1; a <= 5; a++) {
System.out.println(a);
}
// 变量a是定义在for循环内部,超出了for循环的范围,外面无法使用
// System.out.println(a); //错误写法
System.out.println("=========");
int b = 1;
while (b <= 5) {
System.out.println(b);
b++;
}
System.out.println("=============");
System.out.println(b);
}
}
-----------------------------------------------------------------
public class Loop {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 300; i++) {
for (int j = 1; j <= 80; j++) {
count++;
}
}
System.out.println("总人数:" + count);
}
}
----------------------------------------------------------------------------------------
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 + "分");
}
}
}