From 4d9006f641ced947c5dd89086c2cf0f1300c1a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AE=8F=E6=89=AC?= Date: Mon, 5 Dec 2022 18:40:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\347\232\204\347\254\224\350\256\260.md" | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 "14 \345\256\213\345\256\217\346\211\254/20221203\345\205\263\344\272\216if\350\257\255\345\217\245\345\222\214switch\345\210\206\346\224\257\347\273\223\346\236\204\347\232\204\347\254\224\350\256\260.md" diff --git "a/14 \345\256\213\345\256\217\346\211\254/20221203\345\205\263\344\272\216if\350\257\255\345\217\245\345\222\214switch\345\210\206\346\224\257\347\273\223\346\236\204\347\232\204\347\254\224\350\256\260.md" "b/14 \345\256\213\345\256\217\346\211\254/20221203\345\205\263\344\272\216if\350\257\255\345\217\245\345\222\214switch\345\210\206\346\224\257\347\273\223\346\236\204\347\232\204\347\254\224\350\256\260.md" new file mode 100644 index 0000000..f68f176 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20221203\345\205\263\344\272\216if\350\257\255\345\217\245\345\222\214switch\345\210\206\346\224\257\347\273\223\346\236\204\347\232\204\347\254\224\350\256\260.md" @@ -0,0 +1,150 @@ +## 流程控制语句 + +- ##### 分支结构(if,switch) + +- ##### 循环结构(for,while,do...while) + +### 分支结构 + +- #### if语句 + + 1. 格式 + + ```java + //格式1 判断是否满足条件,满足则执行语句体,不满足则不执行语句体,继续执行后面的语句 + if (关系表达式) { + 语句体; + } + + //格式2 判断是否满足条件,满足则执行语句体1,不满足则执行语句体2,继续执行后面的语句 + if (关系表达式) { + 语句体1; + } else { + 语句体2; + } + + //格式3 判断是否满足表达式1,满足则执行语句体1,不满足则判断是否满足表达式2,满足则执行语句体2,不满足继续往下判断,最后若不满足任何一个表达式则执行语句体n+1 + if (关系表达式1) { + 语句体1; + } else if (关系表达式2) { + 语句体2; + } + … + else { + 语句体n+1; + } + ``` + + 2. 示例 + + ```java + //格式1示例:判断年龄是否18,18则可以上网吧 + public class IfDemo { + public static void main(String[] args) { + System.out.println("开始"); + + // 如果年龄大于18岁, 就可以上网吧 + int age = 17; + + if(age >= 18){ + // int a = 10; + System.out.println("可以上网吧"); + } + + System.out.println("结束"); + } + } + + //格式2示例:判断奇偶数 + public class Demo2If { + public static void main(String[] args) { + // 程序判断一个数, 是奇数还是偶数 + int num = 9; + + if(num % 2 == 0){ + System.out.println("偶数"); + }else{ + System.out.println("奇数"); + } + } + } + + //格式3示例:判断成绩再哪个范围,90~100优秀,80~89良好,70~79中等,60~69及格,0~59请努力加油! + public class Demo3If { + public static void main(String[] args){ + int score = 65; + if(score >= 90 && score <= 100){ + System.out.println("优秀"); + }else if (score >= 80 && score <= 89){ + System.out.println("良好"); + }else if (score >= 70 && score <= 79){ + System.out.println("中等"); + }else if (score >= 60 && score <= 69){ + System.out.println("及格"); + }else if (score >= 0 && score <= 59){ + System.out.println("请努力加油"); + }else{ + System.out.println("成绩有误!"); + } + } + } + ``` + +- #### switch语句 + + 1. 格式 + + ```java + switch (表达式) { + case 1: + 语句体1; + break; + case 2: + 语句体2; + break; + ... + default: + 语句体n+1; + break; + } + ``` + + 2. 示例 + + ```java + public static void main(String[] args){ + // 1. 键盘录入星期数据,使用变量接收 + Scanner sc = new Scanner(System.in); + System.out.println("请输入"); + int week = sc.nextInt(); + // 2. 多情况判断,采用switch语句实现 + switch(week){ + // 3. 在不同的case中,输出对应的减肥计划 + case 1: + System.out.println("跑步"); + break; + case 2: + System.out.println("游泳"); + break; + case 3: + System.out.println("慢走"); + break; + case 4: + System.out.println("动感单车"); + break; + case 5: + System.out.println("拳击"); + break; + case 6: + System.out.println("爬山"); + break; + case 7: + System.out.println("好好吃一顿"); + break; + default: + System.out.println("您的输入有误"); + break; + } + } + } + ``` \ No newline at end of file -- Gitee From e08b432fd84e6f92eaf3e8aa0df1d767da596196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AE=8F=E6=89=AC?= Date: Wed, 7 Dec 2022 20:12:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8B=E5=AE=8F=E6=89=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...le\347\232\204\347\254\224\350\256\260.md" | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 "14 \345\256\213\345\256\217\346\211\254/20221306\345\205\263\344\272\216for\345\276\252\347\216\257\343\200\201while\345\276\252\347\216\257\345\222\214do...while\347\232\204\347\254\224\350\256\260.md" diff --git "a/14 \345\256\213\345\256\217\346\211\254/20221306\345\205\263\344\272\216for\345\276\252\347\216\257\343\200\201while\345\276\252\347\216\257\345\222\214do...while\347\232\204\347\254\224\350\256\260.md" "b/14 \345\256\213\345\256\217\346\211\254/20221306\345\205\263\344\272\216for\345\276\252\347\216\257\343\200\201while\345\276\252\347\216\257\345\222\214do...while\347\232\204\347\254\224\350\256\260.md" new file mode 100644 index 0000000..df2c926 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20221306\345\205\263\344\272\216for\345\276\252\347\216\257\343\200\201while\345\276\252\347\216\257\345\222\214do...while\347\232\204\347\254\224\350\256\260.md" @@ -0,0 +1,152 @@ +## for循环 + +- 格式 + + ```java + for (初始化语句;条件判断语句;条件控制语句){ + 循环语句体; + } + //初始化语句: 用于表示循环开启时的起始状态,简单说就是循环开始的时候什么样 + //条件判断语句:用于表示循环反复执行的条件,简单说就是判断循环是否能一直执行下去 + //循环体语句: 用于表示循环反复执行的内容,简单说就是循环反复执行的事情 + //条件控制语句:用于表示循环执行中每次变化的内容,简单说就是控制循环是否能执行下去 + ``` + +- 案例 + + ```java + //需求:在控制台输出1-5和5-1的数据 + public class ForTest01 { + public static void main(String[] args) { + //需求:输出数据1-5 + for(int i=1; i<=5; i++) { + System.out.println(i); + } + System.out.println("--------"); + //需求:输出数据5-1 + for(int i=5; i>=1; i--) { + System.out.println(i); + } + } + } + + //需求:输出1-100之间的偶数和 + public class ForTest01 { + public static void main(String[] args) { + int num = 0; //定义变量接收数据和 + for(int i = 1; i <= 100; i++){ //循环 + if(i % 2 == 0){ //if语句判断是否为偶数 + num += i; //是偶数就加起来 + } + } + System.out.print("1-100之间的偶数和为:" + num); //输出值 + } + } + + //需求:输出所有的水仙花数,每行输出两个 + public class S1 { + public static void main(String[] args) { + int a = 0; //接收水仙花数输出次数 + for (int i = 100; i <= 1000; i++){ + int ge = i % 10; //求出个位上的值 + int shi = i / 10 % 10; //求出十位上的值 + int bai = i / 100 % 10; //求出百位上的值 + if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i){ + System.out.print(i + " "); //输出水仙花数 + a++; //每输出一次a+1 + if (a % 2 == 0){ //每当输出两次就换行 + System.out.println(); + } + } + } + } + } + ``` + +- 注意: + + println:是输出后换行 + + print:输出后不换行 + +## while循环 + +- 格式 + + ```java + 初始化语句; + while (条件判断语句){ + 循环语句体; + 条件控制语句; + } + ``` + +- 案例 + + ```java + //需求:在控制台输出5次"HelloWorld" + public class WhileDemo { + public static void main(String[] args) { + //for循环实现 + for(int i=1; i<=5; i++) { + System.out.println("HelloWorld"); + } + System.out.println("--------"); + //while循环实现 + int j = 1; + while(j<=5) { + System.out.println("HelloWorld"); + j++; + } + } + } + + //需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度? + public class S1 { + public static void main(String[] args) { + int ci = 0; //定义计数器,计算折叠几次,初始值为0 + double zhi = 0.1; //定义纸张厚度 + int shan = 8844430; //定义珠穆朗玛峰高度 + while (shan >= zhi){ + zhi *= 2; + ci++; + } + System.out.println("纸张需要折" + ci + "次才能和珠穆朗玛峰一样高"); + } + } + ``` + +## do...while循环 + +- 格式 + + ```java + 初始化语句; + do { + 循环语句体; + 条件控制语句; + }while(条件判断语句); + ``` + +- 案例 + + ```java + //需求:在控制台输出5次"HelloWorld" + public class DoWhileDemo { + public static void main(String[] args) { + //for循环实现 + for(int i=1; i<=5; i++) { + System.out.println("HelloWorld"); + } + System.out.println("--------"); + //do...while循环实现 + int j = 1; + do { + System.out.println("HelloWorld"); + j++; + }while(j<=5); + } + } + ``` + + \ No newline at end of file -- Gitee