diff --git "a/31 \346\235\216\346\254\243/2022-12-19_222817.png" "b/31 \346\235\216\346\254\243/2022-12-19_222817.png" new file mode 100644 index 0000000000000000000000000000000000000000..df834cf952ba4c3bef7a5e7b1d1f205a275b81cb Binary files /dev/null and "b/31 \346\235\216\346\254\243/2022-12-19_222817.png" differ diff --git "a/31 \346\235\216\346\254\243/20221219 \345\217\202\346\225\260\347\232\204\345\214\272\345\210\253/2022-12-19_222817.png" "b/31 \346\235\216\346\254\243/20221219 \345\217\202\346\225\260\347\232\204\345\214\272\345\210\253/2022-12-19_222817.png" new file mode 100644 index 0000000000000000000000000000000000000000..df834cf952ba4c3bef7a5e7b1d1f205a275b81cb Binary files /dev/null and "b/31 \346\235\216\346\254\243/20221219 \345\217\202\346\225\260\347\232\204\345\214\272\345\210\253/2022-12-19_222817.png" differ diff --git "a/31 \346\235\216\346\254\243/20221219 \346\226\271\346\263\225\345\220\216\347\273\255.md" "b/31 \346\235\216\346\254\243/20221219 \346\226\271\346\263\225\345\220\216\347\273\255.md" new file mode 100644 index 0000000000000000000000000000000000000000..8bd475aaf3d5d5886e8242605a6fe502cb7aabe4 --- /dev/null +++ "b/31 \346\235\216\346\254\243/20221219 \346\226\271\346\263\225\345\220\216\347\273\255.md" @@ -0,0 +1,236 @@ +## 作业: + +编写函数,计算圆的面积和周长,在主函数中接受圆的半径,在自定义函数中计算并输出 + +```java +import java.util.Scanner; + +public class text { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入圆的半径:"); + double r = sc.nextDouble(); + double pi =Math.PI; + System.out.println("这个圆的面积为:"+S(r,pi)); + System.out.println("这个圆的周长为:"+C(r,pi)); + } + public static double S(double a,double pi){ + return a*a*pi; + } + public static double C(double a,double pi){ + return 2*a*pi; + } +} +``` + +在主函数中产生20个0-10之间的随机数,将这20个随机数存入数组,并通过函数计算某个数在这些随机数中出现的次数(这“某个数”是在主函数中由用户输入的) + +```java +import java.util.Random; +import java.util.Scanner; + +public class text1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数:"); + int num2 = sc.nextInt(); + degree(num2); + } + public static int degree(int b){ + Random rand = new Random(); + int[] num1 = new int[20]; + int count =0; + for (int i =0;i< num1.length;i++){ + num1[i]= rand.nextInt(11); + System.out.print("\t"+num1[i]); + if (b==num1[i]){ + count++; + } + } + System.out.println(); + System.out.println("相同次数为:"+count); + return count; + } +} +``` + +在主函数中接收10个数存入数组,在自定义函数中,将该数组中的最大值与第一个元素交换,最小值与最后一个元素交换,然后在主函数中输出交换后的数组 + +```java +import java.util.Arrays; +import java.util.Random; + +public class text2 { + public static void main(String[] args) { + Random r = new Random(); + int[] num = new int[10]; + for (int i = 0; i < num.length; i++) { + num[i] = r.nextInt(100); + } + System.out.println(Arrays.toString(num)); + int max=getMax(num); + int min=getMin(num); + System.out.println(Arrays.toString(num)); + } + public static int getMax(int[] num) { + int max = num[0]; + for (int i = 1; i < num.length; i++) { + if (max < num[i]) { + max = num[i]; + } + } + return num[0]=max; + } + public static int getMin(int[] num) { + int min = num[0]; + for (int i = 1; i < num.length; i++) { + if (min > num[i]) { + min = num[i]; + } + } + return num[9]=min; + } +} +``` + +用自定义函数是实现求某数组元素的和(大小不固定) + +```java +import java.util.Random; + +public class text3 { + public static void main(String[] args) { + Random r = new Random(); + int[] num = new int[10]; + for (int i = 0; i < num.length; i++) { + num[i] = r.nextInt(20)+1; + } + System.out.println("总和为:"+getSun(num)); + } + public static int getSun(int[] a){ + int sum =0; + for (int i = 0; i < a.length; i++){ + sum+=a[i]; + } + return sum; + } +} +``` + +用户输入整数n,计算1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!) + +```java +import java.util.Scanner; + +public class text4 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个整数:"); + int n = sc.nextInt(); + getSum(n); + } + public static int getSum(int n){ + int sum =0; + for (int i = 1; i <= n; i++){ + sum+=i; + } + System.out.println("计算为:"+sum); + return sum; + } +} +``` + +## Math类API + +## 基本类型&引用类型的区别 + +![2022-12-19_222817](20221219 参数的区别/2022-12-19_222817.png) + +## 方法重载 + +方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载 + +##### 多个方法在同一个类中 + +##### 多个方法具有相同的方法名 + +##### 多个方法的参数不相同,类型不同或者数量不同 + +```java +public class MethodDemo { + public static void fn(int a) { + //方法体 + } + public static int fn(double a) { + //方法体 + } +} +public class MethodDemo { + public static float fn(int a) { + //方法体 + } + public static int fn(int a , int b) { + //方法体 + } +} + +``` + +重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式 + +重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值 来判定两个方法是否相互构成重载 + +## 方法的参数传递 + +#### 基本类型 + +基本数据类型的参数,形式参数的改变,不影响实际参数 + +每个方法在栈内存中,都会有独立的栈空间,方法运行结束后就会弹栈消失 + +```java +package com.itheima.param; +public class Test1 { + /* + 方法参数传递为基本数据类型 : + 传入方法中的, 是具体的数值. + */ + public static void main(String[] args) { + int number = 100; + System.out.println("调用change方法前:" + number); + change(number); + System.out.println("调用change方法后:" + number); + } + public static void change(int number) { + number = 200; + } +} +``` + + + +#### 引用类型 + +对于引用类型的参数,形式参数的改变,影响实际参数的值 + +引用数据类型的传参,传入的是地址值,内存中会造成两个引用指向同一个内存的效果,所以 即使方法弹栈,堆内存中的数据也已经是改变后的结果 + +```java +package com.itheima.param; + +public class Test2 { + /* + 方法参数传递为引用数据类型 : + 传入方法中的, 是内存地址. + */ + public static void main(String[] args) { + int[] arr = {10, 20, 30}; + System.out.println("调用change方法前:" + arr[1]); + change(arr); + System.out.println("调用change方法后:" + arr[1]); + } + public static void change(int[] arr) { + arr[1] = 200; } +} +``` +