diff --git "a/25 \346\234\261\346\203\240\345\277\240/20221202 if\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" "b/25 \346\234\261\346\203\240\345\277\240/20221202 if\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..845f6619c13e9a5b79a71560e2e6ca1ac7fbacfa --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20221202 if\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" @@ -0,0 +1,190 @@ +## 笔记 + +## 流程控制语句 + +在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的。所以,我们必须清楚每条语句的执行流程。而且,很多时候要通过控制语句的执行顺序来实现我们想要的功能。 + +### 1 流程控制语句分类(了解) + +​ 顺序结构 + +​ 分支结构(if, switch) + +​ 循环结构(for, while, do…while) + +### 2 顺序结构(了解) + +顺序结构是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码的先后顺序,依次执行,程序中大多数的代码都是这样执行的。 + +顺序结构执行流程图: + +![1545615769372](E:\课件\2img\图片2.png) + +### 3 分支结构之if语句 + +#### 3.1 if语句格式1(理解) + +``` java +格式: +if (关系表达式) { + 语句体; +} +``` + +执行流程: + +①首先计算关系表达式的值 + +②如果关系表达式的值为true就执行语句体 + +③如果关系表达式的值为false就不执行语句体 + +④继续执行后面的语句内容 + +![image-20221206011203299](C:\Users\ZHZ\AppData\Roaming\Typora\typora-user-images\image-20221206011203299.png) + +## 作业 + +一· + +``` java +import java.util.Scanner; + +public class C3 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入年份:"); + int year = sc.nextInt(); + if (year < 0) { + System.out.println("你输入的年份错误"); + } else { + System.out.println("请输入月份:"); + int month = sc.nextInt(); + if (!(month > 0 && month < 12)) { + System.out.println("你输入的月份错误"); + } else { + System.out.println("请输入号数:"); + int day = sc.nextInt(); + if (!(day > 0 && day <= 31)) { + System.out.println("你输入的号数错误"); + } else { + int sum = 0; + switch (month) { + case 12: + sum += 31; + case 11: + sum += 30; + case 10: + sum += 31; + case 9: + sum += 30; + case 8: + sum += 31; + case 7: + sum += 31; + case 6: + sum += 30; + case 5: + sum += 31; + case 4: + sum += 30; + case 3: + sum += 31; + case 2: + if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { + sum += 29; + } else { + sum += 28; + } + case 1: + sum += day; + } + System.out.println(year + "年的" + month + "月" + day + "日是第" + sum + "天"); + } + } + } + } +} +``` + +二· + +``` java +import java.util.Scanner; + +public class C2 { + public static void main(String[] args) { + double price =0; + Scanner sc = new Scanner(System.in); + System.out.println("你想乘坐的出租车类型:"); + String taxi = sc.next(); + System.out.println("你的行车千米数:"); + double kg = sc.nextDouble(); + switch (taxi){ + case "夏利": + if (kg>3){ + price = 3+(kg-3)*2.1; + }else { + price =3; + } + break; + case "富康": + if (kg>3){ + price = 3+(kg-3)*2.4; + }else { + price =4; + } + break; + case "桑塔纳": + if (kg>3){ + price = 3+(kg-3)*2.7; + }else { + price =5; + } + break; + } + System.out.println("应付车费:"+price); + } +} + +``` + +三· + +``` java +import java.util.Scanner; + +public class C1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个字符"); + char character =sc.next().charAt(0); + if (character>=65 && character<=90 || character>=97 && character<=122){ + switch (character){ + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + System.out.println(character+"是韵母"); + break; + default: + System.out.println(character+"是声母"); + } + if (character>=65 && character<=90){ + System.out.println("是大写字母"); + }else{ + System.out.println("是小写字母"); + } + }else{ + System.out.println("你输入的字符不是字母"); + } + } +} +``` + diff --git "a/25 \346\234\261\346\203\240\345\277\240/20221207 for\345\276\252\347\216\257\345\222\214while\345\276\252\347\216\257.md" "b/25 \346\234\261\346\203\240\345\277\240/20221207 for\345\276\252\347\216\257\345\222\214while\345\276\252\347\216\257.md" new file mode 100644 index 0000000000000000000000000000000000000000..4fd642db26ee6352d528b5ec5a4bb4998d6f0e49 --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20221207 for\345\276\252\347\216\257\345\222\214while\345\276\252\347\216\257.md" @@ -0,0 +1,89 @@ +## 循环语句-for循环 + + 循环: + + 循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环 体语句,当反复 执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循 环,否则循环将一直执行下去,形成死循环。 + +for循环格式 + +``` java +for (初始化语句;条件判断语句;条件控制语句) { +循环体语句; +} +``` + +格式解释: + +初始化语句: 用于表示循环开启时的起始状态,简单说就是循环开始的时候什么样 + +条件判断语句:用于表示循环反复执行的条件,简单说就是判断循环是否能一直执行下去 + +循环体语句: 用于表示循环反复执行的内容,简单说就是循环反复执行的事情 + +条件控制语句:用于表示循环执行中每次变化的内容,简单说就是控制循环是否能执行下去 + +执行流程: + +①执行初始化语句 + +②执行条件判断语句,看其结果是true还是false + + 如果是false,循环结束 + + 如果是true,继续执行 + +③执行循环体语句 + +④执行条件控制语句 + +⑤回到②继续 + +## 循环语句-while循环 + +while循环完整格式: + +``` java +初始化语句; +while (条件判断语句) { +循环体语句; +条件控制语句; +} +``` + +while循环执行流程: + +①执行初始化语句 + +②执行条件判断语句,看其结果是true还是false + +如果是false,循环结束 + +如果是true,继续执行 + +③执行循环体语句 + +④执行条件控制语句 + +⑤回到②继续 + +示例代码 + +``` java +public class WhileDemo { +public static void main(String[] args) { +//需求:在控制台输出5次"HelloWorld" +//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++; +} +} +} +``` + diff --git "a/25 \346\234\261\346\203\240\345\277\240/20221209 \346\225\260\347\273\204.md" "b/25 \346\234\261\346\203\240\345\277\240/20221209 \346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..aa1ca206a0732ffd70fbcc1cb5bc7e809dd53cf4 --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20221209 \346\225\260\347\273\204.md" @@ -0,0 +1,74 @@ +``` java +import java.util.Random; +import java.util.Scanner; + + public class Z2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Random r = new Random(); + int a = r.nextInt(90)+10; + int b = 0; + c:while (true){ + System.out.println("请输入10-99的数字"); + int c = sc.nextInt(); + if (c>a){ + System.out.println("猜大了"); + }else if (c == a){ + System.out.println("猜对了"); + break; + }else { + System.out.println("猜小了"); + } + b++; + if (b == 3) { + System.out.println("次数用完,是否继续Y/N"); + char d = sc.next().charAt(0); + switch (d){ + case 'Y': + case 'y': + c = 0; + break; + case 'N': + case 'n': + break c; + default: + System.out.println("错误"); + break c; + } + } + } + } + } +``` + +``` java +import java.util.Arrays; +import java.util.Random; + +public class Z2 { + public static void main(String[] args) { + Random r = new Random(); + int[] arr = new int[10]; + for (int i = 0;imax){ + max = arr[i]; + } + } + System.out.println("最大值为:"+max); + } +} +``` +