diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" "b/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" new file mode 100644 index 0000000000000000000000000000000000000000..994c676e58e3745f3f474280d7fa6692afa92793 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" @@ -0,0 +1,329 @@ +# 分支结构之if和switch语句 + +#### 2022.12.02 + +### 流程控制语句 + +顺序结构 + +分支结构(if,switch) + +循环结构(for, while,do...while) + +### if + +#### if语句1 + +```java +格式: +if (关系表达式){ + 语句体; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入你的年龄:"); + int age = sc.nextInt(); + if (age>=18){ + System.out.println("你已成年,请愉快地玩游戏吧!"); + } + } +} +``` + +#### if语句2 + +```java +格式: +if (关系表达式){ + 语句体1; +}else{ + 语句体2; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入你的年龄:"); + int age = sc.nextInt(); + if (age>=18){ + System.out.println("你已成年,请愉快地玩游戏吧!"); + }else{ + System.out.println("很抱歉,你的年龄未达到要求,不能玩游戏!"); + } + } +} +``` + +#### if语句3 + +```java +格式: +if (关系表达式1){ + 语句体1; +}else if(关系表达式2){ + 语句体2; +} +else{ + 语句体3; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入你的成绩:"); + int score = sc.nextInt(); + if (score >= 0 && score <= 100) { + if (score>=0 && score < 60) { + System.out.println("你的成绩为不合格。"); + } else if (score>=60 && score < 75) { + System.out.println("你的成绩为合格。"); + } else if (score>=75 && score < 90) { + System.out.println("你的成绩为良好。"); + } else { + System.out.println("你的成绩为优秀!"); + } + }else { + System.out.println("请重新输入。"); + } + } +} +``` + +注意:if else这种分支结构是互斥的,众多分支中,只能执行一个,也就是前面已经有一个满足了,后面的就不会执行了。 + +### switch + +```java +switch (表达式1){ + case 1: + 语句体1; + break; + case 2: + 语句体2; + break; + default: + 语句体3; + break; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("今天周几?"); + int week =sc.nextInt(); + switch (week){ + 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; + } + } +} +``` + +注意:switch能做的事,if也能做,反过来不一定。switch只能判断精准的某一个值,if判断的是一个区间一个范围。 + + + +### 字符串的比较 + +```java +import java.util.Scanner; + +public class a5 { + public static void main(String[] args) { + String password="aaa"; + Scanner sc = new Scanner(System.in); + System.out.println("请输入密码:"); + String newpassword=sc.next(); + if (password.equals(newpassword)){ + System.out.println("密码正确"); + } + else{ + System.out.println("密码错误"); + } + } +} +//因为String是引用类型,用equals方法比较是否相等。 +//char n=sc,next(),charAt(0)(输出第几个); +``` + + + +### 作业 + +某市不同车牌的出租车3千米的起步价和计费分别和计费分别为:夏利3元,3千米以外,2.1元/千米;富康4元,3千米以外,2.4元/千米;桑塔纳5元,3千米以外,2.7元/千米。编程实现从键盘输入乘车的车型及行车千米数,输入应付车费。 + +```java +import java.util.Scanner; + +public class a3 { + public static void main(String[] args) { + double pay=0; + Scanner sc = new Scanner(System.in); + System.out.println("请输入乘坐的车型:"); + String car = sc.next(); + System.out.println("请输入行车千米数:"); + double kilometer = sc.nextDouble(); + double kilo=kilometer-3; + switch (car) { + case "夏利": + if (kilometer > 0 && kilometer <= 3) { + pay+=3; + } else { + pay = 3+(kilo*2.1); + } + break; + case "富康": + if (kilometer > 0 && kilometer <= 3) { + pay += 4; + } else { + pay = 4+(kilo*2.4); + } + break; + case "桑塔纳": + if (kilometer > 0 && kilometer <= 3) { + pay += 5; + } else { + pay = 5+(kilo*2.7); + } + break; + } + System.out.println("需要支付:"+pay); + } +} +``` + + + +使用switch和if语句算出今天是今年的第几天。 + +```java +import java.util.Scanner; + +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入今天的日期:"); + int year= sc.nextInt(); + int month=sc.nextInt(); + int day= sc.nextInt(); + int sum=0; + switch (month){ + case 12: + sum+=30; + case 11: + sum+=31; + case 10: + sum+=30; + case 9: + sum+=31; + case 8: + sum+=31; + case 7: + sum+=30; + case 6: + sum+=31; + case 5: + sum+=30; + case 4: + sum+=31; + case 3: + if (year%4==0 && year%100!=0 ||year%400==0){ + sum+=29; + }else{ + sum+=28; + } + case 2: + sum+=31; + case 1: + sum=sum+day; + } + System.out.println(year+"年"+month+"月"+day+"日是本年的第"+sum+"天"); + } +} + +``` + + + +编写一个程序,根据用户输入的一个字符,判断该字符是不是字符,如果是字母,判断该字母是声母还是韵母,是大写字母还是小写字母;如果不是字母,则输出“你输入的字符不是字母”。 + +```java +import java.util.Scanner; + +public class a4 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入一个字符:"); + char a=sc.next().charAt(0); + + if (a>='a'&& a<='z' || a>='A'&& a<='Z'){ + switch (a){ + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + System.out.println(a+"是小写韵母"); + break; + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + System.out.println(a+"是大写韵母"); + break; + case 'b': + case 'c': + case 'd': + case 'g': + case 'h': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + System.out.println(a+"是小写声母"); + break; + default: + System.out.println(a+"是大写声母"); + } + + }else{ + System.out.println("输入的字符不是字母。"); + } + + } +} +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" "b/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..eecdde89a036e21435ea67ffffd0863f5f694d10 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" @@ -0,0 +1,178 @@ +# 循环语句 + +#### 2022.12.06 + +### for循环 + +```java +格式: +for (初始化语句;条件判断语句:条件控制语句){ + 循环体语句; +} +``` + +```java +//连续输出'aaa' +public class f1 { + public static void main(String[] args) { + for (int i=1;i<=5;){ //i定义初始值为1 + System.out.println("aaa"); + } + } +} //输出结果为连续的aaa +//输出1-5 + for (int i=1;i<=5;i++){ + System.out.println(i); + //System.out.print(i); 横向输出 + } +``` + +```java +//输出1-5的和 +public class f { + public static void main(String[] args) { + int sum=0; + for (int i=1;i<=5;i++){ + sum+=i; + } + System.out.println(sum); + } +} +//输出1-100的偶数和 + int sum=0; //定义总值 + for (int i=1;i<=100;i++){ + if (i%2==0){ //判断偶数 + sum+=i; + } + } + System.out.println(sum); +``` + +```java +//水仙花数-例如153,1^3+5^3+3^3=153 + int count=0; //计数 + for (int i =100;i<=999;i++){ + int a=i%10;//个位 + int b=i/10%10;//十位 + int c=i/100%10;//百位 + if(a*a*a+b*b*b+c*c*c==i) { + System.out.print(i+" "); + count++; + if (count % 2==0){ + System.out.println(); //每两个换行 + } + } + } +``` + +### while循环 + +```java +格式: +初始化语句; +while (条件判断语句){ + 循环体语句; + 条件控制语句; +} +``` + +```java +//输出五次aaa +public class w1 { + public static void main(String[] args) { + int i=1; + while (i<=5){ + System.out.println("aaa"); + i++; + } + } +} +``` + +```java +//纸张折叠几次和珠穆朗玛峰一样高 +public class w1 { + public static void main(String[] args) { + int count=0;//折叠的次数 + double paper=0.1;//纸张的初始厚度 + double feng=8844430;//峰高 + while (paper<=feng){ + paper*=2; //每折叠一次,厚度就会加倍 + count++; //折叠的总次数 + } + System.out.println("需要折叠"+count+"次"); + } +} +``` + +### dowhile循环 + +```java +格式: +do{ + 循环体语句; + 条件控制语句; +} while(条件判断语句); +``` + +```java +//输出五次aaa +public class w1 { + public static void main(String[] args) { + int a=1; + do{ + System.out.println("aaa"); + a++; + }while (a<=5); + } +} +``` + +#### 区别 + +for和while是先判断后循环,do...while是先执行一次,然后判断是否继续执行。 + +### 死循环 + +```java +//for + for(;;){ + System.out.println("aa"); + } +//while + while(true){ + System.out.println("hhh"); + } +//do...while + do{ + System.out.println("~"); + }while(true); +``` + +### 跳转控制语句 + +break:跳出循环,结束循环 + +```java +//模拟20岁工作到80岁, 60岁退休. +for (int a=20;a<=80;a++){ + if (a==60){ + break; + } + System.out.println(a+"岁在上班"); + } + +``` + +continue:跳出本次循环,继续下次循环 + +```java +//模拟电梯上行的过程 1-24层, 4层不停. + for (int a=1;a<=24;a++){ + if (a==4){ + continue; + } + System.out.println(a+"层"); + } +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" "b/01 \350\213\217\346\270\205\345\215\216/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" new file mode 100644 index 0000000000000000000000000000000000000000..81ac0a8c151d6156bd8994fd232c197049672315 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221208 \351\232\217\346\234\272\346\225\260\345\222\214\346\225\260\347\273\204.md" @@ -0,0 +1,263 @@ +# 随机数和数组 + +#### 2022.12.08 + +### random随机数 + +1. 导入包 import java.util.Random; + +2. 创建对象 Random r = new Random(); + +3. 产生随机数 int num = r.nextInt(10); + + 解释: 10代表的是一个范围,如果括号写10,产生的随机数就是0-9,括号写20,参数的随 机数则是0-19 + +```java +/* +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 class a { + public static void main(String[] args) { + Random r=new Random(); //创建对象 + for (int i=1;i<=10;i++){ + int num= r.nextInt(10)+1; //创建变量接收,并且给一个范围 + System.out.println(num); + } + } +} +``` + +### 数组 + +——一次性声明大量的数据,且同类型。 + +```JAVA +格式1: +数据类型 [] 变量名; +int []arr; +//定义int类型数组 + +格式2: +数据类型 变量名[]; +int arr[]; +//定义int类型变量 +``` + +#### 数组的初始化 + +1.静态初始化 + +```java +格式: +数据类型[] 变量名=new 数据类型; +int [] arr =new int[]{1,2,3} +简化:int[]{1,2,3} +``` + +2.动态初始化 + +```java +格式: +数据类型[] 数组名 = new 数据类型[数组长度]; +int[] arr = new int[3]; +``` + +#### 数组元素访问 + +```java +格式: +数组名[索引]; +double []={90,85,75};//——0,1,2 +System.out.println(cj[1]);——//输出85 +``` + +```java +怎么获取数组长度(个数) +int num=cj.length; +``` + +#### 遍历 + +就是将数组中的每个元素分别获取出来,就是遍历。遍历也是数组操作中的基石。 + +```java +public class ArrayTest01 { + public static void main(String[] args) { + //定义数组 + int[] arr = {11, 22, 33, 44, 55}; + + //使用通用的遍历格式 + for(int x=0; x max){ + max = arr[i]; + } + } + // 4. 循环结束后, 打印最大值. + System.out.println("max:" + max); + } +} +``` +#### 元素打乱 + +```java +public class a { + public static void main(String[] args) { + int [] num={10,20,30,48,60}; + System.out.println(Arrays.toString(num)); + + for (int i=0;i<5;i++){ + Random ran=new Random(); + int a= ran.nextInt(num.length); + int b= ran.nextInt(num.length); + int c=num[a]; + num[a]=num[b]; + num[b]=c; + } + System.out.println(Arrays.toString(num)); + } +} +``` + + + +### 作业 + +1.猜数游戏 + +```java +import java.util.Random; +import java.util.Scanner; + +public class f1 { + public static void main(String[] args) { + int count=0; + Random r=new Random(); + Scanner sc=new Scanner(System.in); + int num= r.nextInt(100)+1; + om:while (true) { + System.out.println("请输入你猜的数:"); + int a = sc.nextInt(); + count++; + if (a>num){ + System.out.println("大了"); + }else if(amax){ + max=num[j]; + } + } + System.out.println("最大值为:"+max); + + } +} +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221209 \345\212\250\346\200\201\345\210\235\345\247\213\345\214\226\345\222\214\346\225\260\346\215\256\345\206\205\345\255\230.md" "b/01 \350\213\217\346\270\205\345\215\216/20221209 \345\212\250\346\200\201\345\210\235\345\247\213\345\214\226\345\222\214\346\225\260\346\215\256\345\206\205\345\255\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..c23a1b612e100ab678694ea81c034f2a1051bce4 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221209 \345\212\250\346\200\201\345\210\235\345\247\213\345\214\226\345\222\214\346\225\260\346\215\256\345\206\205\345\255\230.md" @@ -0,0 +1,121 @@ +# 动态初始化和数据内存 + +#### 2022.12.09 + +### 动态初始化 + +——数组动态初始化就是只给定数组的长度,由系统给出默认初始化值。 + +格式: + +```java +数据类型[] 数组名 = new 数据类型[数组长度]; +int []arr =new int[3]; +``` + +举例:定义数组并输出 + +```java +package d9; + +import java.util.Scanner; + +public class a1 { + public static void main(String[] args) { + double [] scores=new double[5]; + Scanner sc=new Scanner(System.in); + for (int i=0;imax){ + max=num[i]; + } + } + double min=num[0]; //求最小值 + for (int i=1;i< num.length;i++){ + if (num[i]