From 773826cd0807216767a3dad8ac2f19112e96f9c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com> Date: Tue, 20 Dec 2022 17:45:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\254\241\346\226\271\346\263\225.md" | 31 +- ...\346\263\225(\345\244\215\344\271\240).md" | 316 ++++++++++++++++++ 2 files changed, 330 insertions(+), 17 deletions(-) create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" diff --git "a/03 \350\265\226\345\277\203\345\246\215/20221216 \347\254\254\344\270\200\346\254\241\346\226\271\346\263\225.md" "b/03 \350\265\226\345\277\203\345\246\215/20221216 \347\254\254\344\270\200\346\254\241\346\226\271\346\263\225.md" index eeba4dd..39731eb 100644 --- "a/03 \350\265\226\345\277\203\345\246\215/20221216 \347\254\254\344\270\200\346\254\241\346\226\271\346\263\225.md" +++ "b/03 \350\265\226\345\277\203\345\246\215/20221216 \347\254\254\344\270\200\346\254\241\346\226\271\346\263\225.md" @@ -263,19 +263,18 @@ import java.util.Scanner; public class D1 { public static void main(String[] args) { - Scanner num1 = new Scanner(System.in); + Scanner sc = new Scanner(System.in); System.out.println("请输入第一个数字"); - int a = num1.nextInt(); - - Scanner num2 = new Scanner(System.in); + int a = sc.nextInt(); + System.out.println("请输入第二个数字"); - int b = num2.nextInt(); + int b = sc.nextInt(); - int c = getSum(a,b); + int c = getMax(a,b); System.out.println("最大值是:" + c); } - public static int getSum(int a,int b){ + public static int getMax(int a,int b){ return a>b?a:b; } } @@ -289,25 +288,23 @@ import java.util.Scanner; public class D2 { public static void main(String[] args) { - Scanner num1 = new Scanner(System.in); + Scanner sc = new Scanner(System.in); System.out.println("请输入数字X"); - int X = num1.nextInt(); + int x = sc.nextInt(); - Scanner num2 = new Scanner(System.in); System.out.println("请输入数字Y"); - int Y = num2.nextInt(); + int y = sc.nextInt(); - Scanner num3 = new Scanner(System.in); System.out.println("请输入数字Z"); - int Z = num3.nextInt(); + int z = sc.nextInt(); - int S =getSum(X,Y,Z); - System.out.println("立方和S=" + S); + int s =Num(x,y,z); + System.out.println("立方和S=" + s); } - public static int getSum(int X,int Y,int Z){ - return X=X*X*X+Y*Y*Y+Z*Z*Z; + public static int Num(int x,int y,int z){ + return x=x*x*x+y*y*y+z*z*z; } } ``` diff --git "a/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" "b/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" new file mode 100644 index 0000000..d00823e --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" @@ -0,0 +1,316 @@ +### 方法 + +**方法的定义与调用** + +```java +定义: +public static void method ( ) { + // 方法体; +} +//method是方法名 + +调用: +method(); +``` + +*要先定义后调用,否者程序会报错* + +**带参数方法定义和调用** + +*参数:由数据类型和变量名组成 - 数据类型 变量名* + +*参数范例:int a* + +```java +参数 +public static void isEvenNumber(int number){ +... +} +public static void getMax(int num1, int num2){ +... +} + +/* + isEvenNumber和getMax是方法名 + int number和int num1、int num2是参数 + + 方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错 + 方法定义时,多个参数之间使用逗号( ,)分隔 +*/ + +调用 +isEvenNumber(10); +getMax(10,20); + +//isEvenNumber和getMax是方法名 +//10,20是参数 +``` + +**形参和实参** + +1. 形参:方法定义中的参数 + + 等同于变量定义格式,例如:int number + +2. 实参:方法调用中的参数 + + 等同于使用变量或常量,例如: 10 number + +**带返回值方法定义和调用** + +```java +定义 +public static boolean isEvenNumber( int number ) { + return true ; +} +public static int getMax( int a, int b ) { + return 100 ; +} +/* + 格式: + public static 数据类型 方法名 ( 参数 ) { + return 数据 ; + } +*/ + +调用 +isEvenNumber ( 5 ) ; +boolean flag = isEvenNumber ( 5 ); +/* + 方法名 ( 参数 ) ; + 数据类型 变量名 = 方法名 ( 参数 ) ; +*/ +``` + +*方法的返回值通常会使用变量接收,否则该返回值将无意义* + +**方法的通用格式** + +```java +public static 返回值类型 方法名(参数) { + 方法体; + return 数据 ; +} +``` + +**方法重载** + +方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载 + +多个方法在同一个类中 + +多个方法具有相同的方法名 + +多个方法的参数不相同,类型不同或者数量不同 + +注意: + +重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式 + +重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值 来判定两个方法是否相互构成重载 + +```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) { + //方法体 + } +} + +错误示范 +public class MethodDemo { + public static void fn(int a) { + //方法体 + } + public static int fn(int a) { /*错误原因:重载与返回值无关*/ + //方法体 + } +} +public class MethodDemo01 { + public static void fn(int a) { + //方法体 + } +} +public class MethodDemo02 { + public static int fn(double a) { /*错误原因:这是两个类的两个fn方法*/ + //方法体 + } +} +``` + +### 作业 + +编写函数,计算圆的面积和周长,在主函数中接受圆的半径,在自定义函数中计算并输出 + +```java +import java.util.Scanner; + +public class D1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入圆的半径"); + double r = sc.nextDouble(); + + System.out.println("这个圆的周长是" + c(r)); + System.out.println("这个圆的面积是" + s(r)); + } + + public static double c(double r){ + return 2*3.14*r; + } + + public static double s(double r){ + return 3.14*r*r; + } +} +``` + +在主函数中产生20个0-10之间的随机数,将这20个随机数存入数组,并通过函数计算某个数在这些随机数中出现的次数(这“某个数”是在主函数中由用户输入的) + +```java +public class D2 { + public static void main(String[] args) { + + int [] num = new int[20]; + + Random random = new Random(); + for(int i=0;i<20;i++){ + num[i]= random.nextInt(11); + } + + Scanner sc = new Scanner(System.in); + System.out.println("请输入你想查找的数"); + int a = sc.nextInt(); + + search(num,a); + } + + public static void search(int[] num,int a){ + int x=0; + for(int j=0;j < num.length;j++){ + if(a==num[j]){ + x++; + } + } + System.out.println(a + "在随机数组当中出现了" + x + "次"); + } +} +``` + +在主函数中接收10个数存入数组,在自定义函数中,将该数组中的最大值与第一个元素交换,最小值与最后一个元素交换,然后在主函数中输出交换后的数组 + +```java +import java.util.Scanner; + +public class D3 { + public static void main(String[] args) { + + int[] num = new int[10]; + + Scanner sc = new Scanner(System.in); + for (int i = 0; i < num.length; i++) { + System.out.println("请输入" + i+1 + "个数"); + num[i] = sc.nextInt(); + } + + arr(num); + } + + public static void arr(int[] num){ + int max=num[0]; + int min=num[0]; + + for (int i = 0; i < num.length; i++) { + if(max num[i]) { + min = num[i]; + } + } + + for (int i=0;i< num.length;i++){ + if (num[i]==max){ + int exchange=num[0]; + num[0]=max; + num[i]=exchange; + } + if (num[i]==min){ + int exchange=num[num.length-1]; + num[num.length-1]=min; + num[i]=exchange; + } + } + + System.out.println(Arrays.toString(num)); + } +} + + +``` + +用自定义函数是实现求某数组元素的和(大小不固定) + +```java +import java.util.Scanner; + +public class D4 { + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + System.out.println("请问数组的长度是多少?"); + int [] num = new int[sc.nextInt()]; + + for(int i=0;i < num.length;i++){ + System.out.println("请输入第" + (i+1) + "个元素" ); + num[i]= sc.nextInt(); + } + + arr(num); + } + + public static void arr(int[] num){ + int sum=0; + for(int i=0;i< num.length;i++){ + sum+=num[i]; + } + System.out.println("数组元素和为" + sum); + } +} + +``` + +用户输入整数n,计算1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!) + +```java +import java.util.Scanner; + +public class D5 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入数字n"); + int n= sc.nextInt(); + + System.out.println("1!+(1!+2!)+(1!+2!+3!)+…..+(1!+2!+…n!)=" + num(n)); + } + + public static int num(int n){ + int sum=1; + for(int i=1;i<=n;i++){ + sum=sum*i; + } + return sum; + } +} +``` + -- Gitee