1. 流程控制
1.1 分类
-
顺序结构 从上往下依次执行
-
选择结构 分支语句
if swtch
-
循环结构 循环语句
for while do{}while;
2. 顺序结构
2.1 流程图
2.2 demo
System.out.println("start");
System.out.println("A");
System.out.println("B");
System.out.println("end");
3. 分支结构
3.1 if判断
-
语法
if (关系表达式) { 执行语句; }
-
流程图
-
demo
int a = 10; int b = 20; if (a == b) { System.out.println("a = b"); } int c = 10; if (a == c) { System.out.println("a = c"); }
3.2 if {} else {} 选择结构
-
语法
if (关系表达式) { 执行语句; } else { 执行语句; }
-
流程图
-
demo
int a = 10; int b = 20; if (a == b) { System.out.println("a = b"); } else { System.out.println("a != b"); }
-
奇偶数判断
a % 2 == 0
Scanner sc = new Scanner(System.in); System.out.println("输入一个整数"); int a = sc.nextInt(); if (a % 2 == 0){ System.out.println(a + "是偶数"); } else { System.out.println(a + "是奇数"); }
3.3 if (){} elseif () {} ··· else{}多分支结构
-
语法
if (关系表达式1) { 执行语句1; } else if (关系表达式2) { 执行语句2; } else if (关系表达式3) { 执行语句3; } else { 执行语句; }
-
流程图
-
demo
Scanner sc = new Scanner(System.in); System.out.println("输入1~7中的整数"); int a = sc.nextInt(); if (a == 1) { System.out.println("星期一"); } else if (a == 2) { System.out.println("星期二"); } else if (a < 8 & 0 < a) { System.out.println("星期三,四,五,六,日"); } else { System.out.println("输入不合法"); }
-
分数选择
测试数据:正确数据 边界数据 错误数据
Scanner sc = new Scanner(System.in); System.out.println("输入0~100的整数"); int score = sc.nextInt(); if (score < 0 || score > 100) { System.out.println("输入错误"); } else if (score > 94) { System.out.println("分数大于94小于等于100"); } else if (score > 89) { System.out.println("分数大于89小于等于94"); } else if (score > 79) { System.out.println("分数大于79小于等于80"); } else { System.out.println("分数小于80"); }
3.4 switch语句
-
语法
switch (变量) { case 值1: 执行语句1; break; case 值2: 执行语句2; break; default: System.out.println("a !=1 && a != 2"); break; }
Note
- 变量类型 byte short int char jdk7以后String jdk15以后枚举
- break结束判断,必须要写否则发生case穿透
-
流程图
-
demo
int a = 1; switch (a) { case 1: System.out.println("a = 1"); break; case 2: System.out.println("a = 2"); break; default: System.out.println("a !=1 && a != 2"); }
-
四季判断
case穿透
Scanner sc = new Scanner(System.in); System.out.println("输入1-12整数"); int season = sc.nextInt(); switch (season) { case 12: // System.out.println("春季"); case 1: case 2: System.out.println("春季"); break; case 3: case 4: case 5: System.out.println("夏季"); break; case 6: case 7: case 8: System.out.println("秋季"); break; case 9: case 10: case 11: System.out.println("冬季"); break; default: System.out.println("输入不合法"); break; }
4. 循环结构
4.1 循环结构
-
组成
- 初始化语句
int a=1
- 条件判断语句
a < 12
- 循环体语句
System.out.println(a)
- 条件控制语句
a++
- 初始化语句
-
流程图
4.2 for循环
-
语法
for (初始化语句;条件判断语句;条件控制语句){ 循环体语句; }
-
demo
for (int i = 0; i < 5; i++) { System.out.println(i); }
-
累加求和
int sum100 = 0; for (int i = 0; i < 101; i++) { sum100 += i; } System.out.println(sum100);
-
累加偶数和
int sum100 = 0; for (int i = 0; i < 101; i++) { if (i % 2 == 0) { sum100 += i; } } System.out.println(sum100);
-
累乘求积
数字太大 int long都放不下,结果为0
double accumulate = 1; // 数字太大 int long都放不下,结果为0 for (int j = 1; j < 101; j++) { accumulate *= j; } System.out.println(accumulate);
-
水仙花数
所有位上的数的立方的和为原来的数
for (int i = 1; i < 1000 ; i++) { // 个位 int ge = i % 10; // 百位 int bai = i / 100; // 十位 int shi = (i / 10 % 10); if (i == (ge*ge*ge + shi*shi*shi + bai*bai*bai)){ System.out.println(i); } }
note: 获取某一位 整除移到该位 求余取该数
12345 取2 12345/1000=12 12%10=2
4.3 while循环
-
语法
初始化语句; while (条件判断语句){ 循环体语句; 条件控制语句; }
-
demo
int i = 1; while (i < 6) { System.out.println(i); ++i; }
-
纸张折叠
珠峰 8844.43m 纸张0.1mm
double paper = 0.1; double zhf = 8844430; int count = 0; while (paper < zhf) { paper *= 2; count++; } System.out.println(count);
Note 知道循环次数的用for 知道结束条件用while
4.4 do {} while () 循环
-
语法
分号不能忘
初始化语句; do { 循环体语句; 条件控制语句; } while (条件判断语句);
-
demo
int i = 1; do { System.out.println(i); ++i; } while (i < 6);
4.5 三种循环的区别
-
do···while 先执行一次再判断 ,for和while先判断再执行
-
死循环
for (;;){ System.out.println("for"); } while (true) { System.out.println("while"); } do { System.out.println("d···while"); } while (true);
5. 跳转控制 continue break
5.1 概述
continue:跳过本次循环,执行后面的循环
break:终止整个循环
5.2 continue
for (int i = 1; i < 11; i++) {
if (i % 3 == 0) {
continue; // 3的倍数跳过
}
System.out.println(i);
}
// 1 2 4 5 7 8 10
5.3 break
for (int i = 1; i < 11; i++) {
if (i % 3 == 0) {
break; // 3结束整个循环
}
System.out.println(i);
}
// 1 2
6 循环嵌套
6. 1 语句可以嵌套
- 分支结构 循环结构 都可以看成语句块,都可以进行嵌套
- 分支嵌套 循环嵌套···
6.2 23时59分59秒循环
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 60; j++) {
for (int k = 0; k < 60; k++) {
System.out.println(i + " h " + j + " min " + k + " s");
}
}
}
7. Random类 获取随机数
7.1 实例化
import java.util.Random;
Random r = new Random();
int j = r.nextInt(10);
7.2 方法
- 整数
int j = r.nextInt(10);
[0,9)
7.3 demo
import java.util.Random;
import java.util.Scanner;
public class DemoGuessNumber {
public static void main(String[] args) {
Random r = new Random();
int target = r.nextInt(100) + 1;
Scanner sc = new Scanner(System.in);
System.out.println("输入1-100的整数");
int guess = sc.nextInt();
while (guess != target) {
if (guess < target){
System.out.println("猜小了");
} else if (guess > target) {
System.out.println("猜大了");
}
guess = sc.nextInt();
}
System.out.println("猜对了");
}
}
/*
输入1-100的整数
50
猜小了
75
猜大了
62
猜大了
56
猜对了
*/