diff --git "a/13 \345\274\240\345\276\267\345\272\267/20221205 if\344\270\216switch\345\276\252\347\216\257.md" "b/13 \345\274\240\345\276\267\345\272\267/20221205 if\344\270\216switch\345\276\252\347\216\257.md" new file mode 100644 index 0000000000000000000000000000000000000000..a037710590f2863c0628eedb0390a12e2089ce4c --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20221205 if\344\270\216switch\345\276\252\347\216\257.md" @@ -0,0 +1,298 @@ +## if + +if + +执行流程: + +①首先计算关系表达式的值 + +②如果关系表达式的值为true就执行语句体 + +③如果关系表达式的值为false就不执行语句体 + +④继续执行后面的语句内容 + +```java +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("结束"); + } +} +``` + +if加else + +执行流程: + +①首先计算关系表达式的值 + +②如果关系表达式的值为true就执行语句体1 + +③如果关系表达式的值为false就执行语句体2 + +④继续执行后面的语句内容 + +```java +public class Demo2If { + public static void main(String[] args) { + // 程序判断一个数, 是奇数还是偶数 + int num = 9; + + if(num % 2 == 0){ + System.out.println("偶数"); + }else{ + System.out.println("奇数"); + } + } +} +``` + +if 加 else 再加else + +执行流程: + +①首先计算关系表达式1的值 + +②如果值为true就执行语句体1;如果值为false就计算关系表达式2的值 + +③如果值为true就执行语句体2;如果值为false就计算关系表达式3的值 + +```java +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("成绩有误!"); + } + } +} +``` + +## swich + +执行流程: + +- [ ] 首先计算出表达式的值 + +- [ ] 其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到 + +break就会结束。 + +- [ ] 最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉 + +```java +public class Demo3If { + public static void main(String[] args){ +switch (表达式) { + case 1: + //语句体1; + break; + case 2: + //语句体2; + break; + ... + default: + //语句体n+1; + break; + } + } +} +``` + +switch 加 case + +概述 : 如果switch语句中,case省略了break语句, 就会开始case穿透 + +```java +public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("请输入星期数:"); + int week = sc.nextInt(); + + switch(week){ + case 1: + case 2: + case 3: + case 4: + case 5: + System.out.println("工作日"); + break; + case 6: + case 7: + System.out.println("休息日"); + break; +default: + System.out.println("您的输入有误"); + break; + } + } +} +``` + +## 作业1 + +题目:某市出租车3km起步价和计费方式:夏利3元,3km以外,每公里2.1元、富康4元,3km以外,每公里2.4元、桑塔纳5元,3km以外,每公里2.7元 + +```java +import java.util.Scanner; + +public class A1 { + public static void main(String[] args) { + Scanner cs = new Scanner(System.in); + double money = 0; + System.out.println("请输入乘坐的车型:"); + String che = cs.next(); + System.out.println("请输入行车公里数:"); + int gl = cs.nextInt(); + switch (che){ + case "夏利": + if (gl>3){ + money=gl*2.1; + }else { + money=gl*3; + } + case "富康": + if(gl>3){ + money=gl*2.4; + }else { + money=gl*4; + } + case "桑塔纳": + if(gl>3){ + money=gl*2.7; + }else { + money=gl*5; + } + } + System.out.println("需要支付"+money); + } +} +``` + +## 作业2 + +题目:使用switch和if语句算出今天是今年的第几天。 + +``` +import java.util.Scanner; + +public class A2 { + public static void main(String[] args) { + Scanner cs = new Scanner(System.in); + System.out.println("请输入年份:"); + int year = cs.nextInt(); + if (year<=0){ + System.out.println("你输入的年份有误"); + }else { + System.out.println("请输入月份:"); + int month = cs.nextInt(); + if (!(month>=1&&month<=12)){ + System.out.println("你输入的月份有误"); + }else { + System.out.println("请输入号数:"); + int sun = cs.nextInt(); + if (!(month>=1&&month<=31)){ + System.out.println("输入的号数有误"); + } else{ + int s = 0; + switch (month){ + case 12: + s += 30; + case 11: + s += 31; + case 10: + s += 30; + case 9: + s += 31; + case 8: + s += 31; + case 7: + s += 30; + case 6: + s += 31; + case 5: + s += 30; + case 4: + s += 31; + case 3: + if (year%4==0&&year%100!=0||year%400==0){ + s += 29; + }else { + s += 28; + } + case 2: + s += 31; + case 1: + s += sun; + } + System.out.println(year+"年的"+month+"月的"+sun+"日是第"+s+"天"); + } + + } + } + } +} +``` + +## 作业3 + +题目:编写一个程序,根据用户输入的一个字符,判断该字符是不是字母,如果是字母,判断该字母是声母还是韵母,是大写字母还是小写字母;如果不是字母,则输出“你输入的字符不是字母” + +``` +import java.util.Scanner; + +public class A2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个字符:"); + char a = sc.next().charAt(0); + if (a>=65 && a<=90 || a>=97 && a<=122){ + if (a>=65 && a<=90){ + System.out.println("大写字母"); + }else { + System.out.println("小写字母"); + } + switch (a){ + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + System.out.println("韵母"); + break; + default: + System.out.println("声母"); + break; + } + }else { + System.out.println("你输入的字符不是字母"); + } + } +} +``` + diff --git "a/13 \345\274\240\345\276\267\345\272\267/20221208 for\344\270\216while dowhile\345\276\252\347\216\257 .md" "b/13 \345\274\240\345\276\267\345\272\267/20221208 for\344\270\216while dowhile\345\276\252\347\216\257 .md" new file mode 100644 index 0000000000000000000000000000000000000000..5c1398d03a396e76a9b37ff2855738b16d6fcba2 --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20221208 for\344\270\216while dowhile\345\276\252\347\216\257 .md" @@ -0,0 +1,164 @@ +## for循环 + +- 循环: + +​ 循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环 体语句,当反复 执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循 环,否则循环将一直执行下去,形 成死循环。 + +for循环格式: + +``` +for (初始化语句;条件判断语句;条件控制语句) { + 循环体语句; +} +``` + +- 格式解释: + +初始化语句: 用于表示循环开启时的起始状态,简单说就是循环开始的时候什么样 + +条件判断语句:用于表示循环反复执行的条件,简单说就是判断循环是否能一直执行下去 + +循环体语句: 用于表示循环反复执行的内容,简单说就是循环反复执行的事情 + +条件控制语句:用于表示循环执行中每次变化的内容,简单说就是控制循环是否能执行下去 + +- 执行流程: + +①执行初始化语句 + +②执行条件判断语句,看其结果是true还是false + +如果是false,循环结束 + + 如果是true,继续执行 + +③执行循环体语句 + +④执行条件控制语句 + +⑤回到②继续 + +##### for循环案例-输出数据1-5和5-1 + +```java +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); + } +   } +} +``` + +##### for循环案例-求1-100偶数和 + +```java +public class ForTest03 { +    public static void main(String[] args) { + //求和的最终结果必须保存起来,需要定义一个变量,用于保存求和的结果,初始值为0 + + int sum = 0; +//对1-100的数据求和与1-5的数据求和几乎完全一样,仅仅是结束条件不同 + + for(int i=1; i<=100; i++) { + //对1-100的偶数求和,需要对求和操作添加限制条件,判断是否是偶数 + + if(i%2 == 0) { + sum += i; + } + } + //当循环执行完毕时,将最终数据打印出来 + + System.out.println("1-100之间的偶数和是:" + sum); +   } +} +``` + +##### for循环案例-水仙花数 + +解释:什么是水仙花数? + +水仙花数,指的是一个三位数,个位、十位、百位的数字立方和等于原数 + +思路: + +\1. 获取所有的三位数,准备进行筛选,最小的三位数为100,最大的三位数为999,使用for循环 获取 + +\2. 获取每一个三位数的个位,十位,百位,做if语句判断是否是水仙花数 + +```java +public class ForTest04 { +    public static void main(String[] args) { + //输出所有的水仙花数必然要使用到循环,遍历所有的三位数,三位数从100开始,到999结 +束 + + for(int i=100; i<1000; i++) { + //在计算之前获取三位数中每个位上的值 + + int ge = i%10; + int shi = i/10%10; + int bai = i/10/10%10; + + //判定条件是将三位数中的每个数值取出来,计算立方和后与原始数字比较是否相等 + + if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i) { + //输出满足条件的数字就是水仙花数 + + System.out.println(i); + } + } +   } +} + +``` + +## while循环 + +- while循环完整格式: + +初始化语句; + +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/13 \345\274\240\345\276\267\345\272\267/20221209 \350\267\263\350\275\254\346\216\247\345\210\266\345\222\214\351\232\217\346\234\272\346\225\260\344\270\216\346\225\260\347\273\204.md" "b/13 \345\274\240\345\276\267\345\272\267/20221209 \350\267\263\350\275\254\346\216\247\345\210\266\345\222\214\351\232\217\346\234\272\346\225\260\344\270\216\346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..6aed470e2e52af59e7ede1ed87e4d4afcb6868da --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20221209 \350\267\263\350\275\254\346\216\247\345\210\266\345\222\214\351\232\217\346\234\272\346\225\260\344\270\216\346\225\260\347\273\204.md" @@ -0,0 +1,144 @@ +## 跳转控制 + +- 跳转控制语句(break) + +​ 跳出循环,结束循环 + +- 跳转控制语句(continue) + +​ 跳过本次循环,继续下次循环 + +- 注意: continue只能在循环中进行使用! + + ```java + public class Demo1Continue { + /* + continue : 跳过某次循环体内容的执行 + 注意:使用是基于条件控制, 在循环内部使用. + 需求: 模拟电梯上行的过程 1-24层, 4层不停. + */ + public static void main(String[] args){ + for(int i = 1; i <= 24; i++){ + if(i == 4){ + continue; + } + System.out.println(i + "层到了~"); + } + } + } + ``` + + + +## 随机数 + +概述: + + Random类似Scanner,也是Java提供好的API,内部提供了产生随机数的功能 + +API后续课程详细讲解,现在可以简单理解为Java已经写好的代码 + +使用步骤: + +1. 导入包 + + import java.util.Random; + + 2. 创建对象 + + Random r = new Random(); + + 3. 产生随机数 + + int num = r.nextInt(10); + + 解释: 10代表的是一个范围,如果括号写10,产生的随机数就是0-9,括号写20,参数的随 机数则是0-19 + + ```java + import java.util.Random; + public class Demo1Random { + /* + Random : 产生随机数 + 1. 导包 : import java.util.Random; + 导包的动作必须出现在类定义的上面 + 2. 创建对象 : Random r = new Random(); + 上面这个格式里面,r 是变量名,可以变,其他的都不允许变 + 3. 获取随机数 : int number = r.nextInt(10); //获取数据的范围:[0,10) 包 + 括0,不包括10 + 上面这个格式里面,number是变量名,可以变,数字10可以变。其他的都不 + 允许变 + 需求: 产生随机数1-10之间的 + */ + public static void main(String[] args){ + // 2. 创建对象 + Random r = new Random(); + for(int i = 1; i <= 10; i++){ + // 3. 获取随机数 + int num = r.nextInt(10) + 1; // 1-10 + System.out.println(num); + } + } + } + ``` + + + +## 数组 + +数组就是存储数据长度固定的容器,存储多个数据的数据类型要一致。 + +数据类型格式 + +第一种: + +1. int[] arr ; + +第二种: + +1. int arr[]; + + + +## 作业 1 + +```java +import java.util.Random; +import java.util.Scanner; +public class D2 { + public static void main(String[] args) { + Random cs = new Random(); + int ruan = cs.nextInt(100); + Scanner rua = new Scanner(System.in); + while (true){ + System.out.println("请输入你的数字:"); + int gg = rua.nextInt(); + if (gg>ruan){ + System.out.println("你猜大了:"); + }else if (ggda){ + da=arr[i]; + } + } + System.out.println("最大为:"+da); + } +} +``` + diff --git "a/13 \345\274\240\345\276\267\345\272\267/20221213 \346\225\260\347\273\204.md" "b/13 \345\274\240\345\276\267\345\272\267/20221213 \346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..5e034e04cc41a0594017549d62e05eda710df1c3 --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20221213 \346\225\260\347\273\204.md" @@ -0,0 +1,35 @@ +```java +import java.util.Random; + +public class A1 { + public static void main(String[] args) { + Random s = new Random(); + int[] one = new int[10]; + for (int i = 0;imax){ + max=one[i]; + } + } + int min = one[0]; + for (int i = 1; i< one.length;i++){ + if (one[i]>min){ + min=one[i]; + } + } + int sun = one[0]; + for (int i = 1; i< one.length;i++){ + sun+=one[i]; + } + int p = (sun - max - min)/(one.length-2); + System.out.println("评委打分:"+p); + } +} +``` +