01方法的概述
02方法的定义和调用
package com.itheima.method1;public class Demo1Method { /* 方法的定义格式: public static void 方法名 (){ 方法体 } 方法的调用格式: 方法名(); 注意: 方法与方法之间是平级关系, 不能嵌套定义 */ public static void main(String[] args) { eat(); } public static void eat (){ study(); System.out.println("吃饭"); } public static void study(){ System.out.println("学习"); }}
03方法的调用过程
04方法练习-奇偶数判断
package com.itheima.method1;public class Demo2Method { /* 需求:设计一个方法method,方法中定义一个变量(数值随意) 判断这个变量是奇数还是偶数,并在main方法中调用method。 1. 定义method方法 2. 方法中定义变量, 使用if语句判断是奇数还是偶数 3. main方法中调用method */ public static void main(String[] args) { // 3. main方法中调用method method(); } // 1. 定义method方法 public static void method(){ // 2. 方法中定义变量, 使用if语句判断是奇数还是偶数 int num = 11; if(num % 2 == 0){ System.out.println("偶数"); }else{ System.out.println("奇数"); } }}
05带参方法的定义和调用
package com.itheima.method2;public class Demo1Method { /* 带参数方法的定义格式: public static void 方法名 ( 参数 ) { … … } public static void 方法名 ( 数据类型 变量名 ) { … … } 带参数方法的调用格式: 方法名 ( 参数 ) ; 方法名 ( 变量名/常量值 ) ; tips: 参数可以是一个, 也可以是多个. 需求: 判断一个数是奇数还是偶数 */ public static void main(String[] args) { isEvenNumber(10); } public static void isEvenNumber(int num){ if(num % 2 == 0){ System.out.println("偶数"); }else{ System.out.println("奇数"); } }}
06形参和实参
07带参方法的练习-打印n-m之间所有的奇数
package com.itheima.method2;public class Demo2Method { /* 需求:设计一个方法(print) 用于打印 n 到 m 之间所有的奇数 思路: 1:定义方法,名称为print 2:为方法添加两个int类型的形参,准备接受调用者传递过来的实参 3:方法中设计for循环,循环从n开始,到m结束 4:循环中加入if判断,是奇数,则打印 5:main方法中调用print方法,传入两个实际参数 */ public static void main(String[] args) { // 5:main方法中调用print方法,传入两个实际参数 print(10,20); } //1:定义方法,名称为print // 2:为方法添加两个int类型的形参,准备接受调用者传递过来的实参 public static void print(int n, int m){ System.out.println(n + "到" + m + "之间的奇数为:"); // 3:方法中设计for循环,循环从n开始,到m结束 for(int i = n; i <= m; i++){ // 4:循环中加入if判断,是奇数,则打印 if(i % 2 == 1){ System.out.println(i); } } }}
08带返回值方法的定义和调用
package com.itheima.method3;public class Demo1Method { /* 带返回值方法的定义格式: public static 数据类型 方法名 ( 参数 ) { return 数据 ; } 方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错 带返回值方法的调用格式: 数据类型 变量名 = 方法名 ( 参数 ) ; 需求: 定义一个方法, 计算两个整数相加的和 */ public static void main(String[] args) { int num = add(10,20); System.out.println(num); } public static int add(int a, int b){ int c = a + b; return c; }}
09带返回值方法的练习-求两个数的最大值
package com.itheima.method3;public class Demo2Method { /* 需求:设计一个方法可以获取两个数的较大值,数据来自于参数 1. 定义一个方法,声明两个形参接收计算的数值,求出结果并返回 2. 使用 if 语句 得出 a 和 b 之间的最大值,根据情况return具体结果 3. 在main()方法中调用定义好的方法并使用 【 变量保存 】 */ public static void main(String[] args) { // 3. 在main()方法中调用定义好的方法并使用 【 变量保存 】 System.out.println(getMax(10,20)); // 输出调用 int result = getMax(10,20); System.out.println(result); for(int i = 1; i <= result; i++){ System.out.println("HelloWorld"); } } // 方法可以获取两个数的较大值 public static int getMax(int a, int b){ if(a > b){ return a; }else{ return b; } }}
10方法通用格式
11方法的注意事项
package com.itheima.method4;public class Demo1Method { public static void main(String[] args) { print(10,20); } public static void print(int n, int m){ if(m < n){ System.out.println("您传入的数据有误, 请检查"); // return; 可以用于结束方法, 也就是将方法从栈内存中弹出去, 该过程称之为方法的弹栈 return; // System.out.println(); 无法执行的代码, 无效代码 // 问题: return语句下面, 不是不能写代码吗? } System.out.println(n + "到" + m + "之间的奇数为:"); for(int i = n; i <= m; i++){ if(i % 2 == 1){ System.out.println(i); } } }}
12方法重载
方法名相同,同一位上参数列表不同
13方法重载练习
package com.itheima.overload;public class Demo1Overload { /* 需求:使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long) */ public static void main(String[] args) { short a = 10; short b = 20; System.out.println(compare(a,b)); } public static boolean compare (int a, int b){ return a == b; } public static boolean compare (byte a, byte b){ return a == b; } public static boolean compare (short a, short b){ return a == b; } public static boolean compare (long a, long b){ return a == b; }}
14方法参数传递基本数据类型
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; }}
15方法参数传递引用数据类型
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; }}
16数组遍历
package com.itheima.test;public class Test1 { /* 需求:设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11, 22, 33, 44, 55] 思路: 1.定义一个数组,用静态初始化完成数组元素初始化 2.定义一个方法,对数组进行遍历 3.遍历打印的时候,数据不换行 4.调用遍历方法 */ public static void main(String[] args) { // 1.定义一个数组,用静态初始化完成数组元素初始化 int[] arr = {11, 22, 33, 44, 55}; // 4.调用遍历方法 printArray(arr); System.out.println("另外一段代码逻辑 "); } /* 2.定义一个方法,对数组进行遍历 1, 参数 int[] arr 2, 返回值类型 void */ public static void printArray(int[] arr){ System.out.print("["); for (int i = 0; i < arr.length; i++) { if(i == arr.length -1){ // 如果满足条件, 说明是最后一个元素, 最后一个元素, 特殊处理 System.out.println(arr[i] + "]"); }else{ // 3.遍历打印的时候,数据不换行 System.out.print(arr[i] + ", "); } } }}
17获取数组最大值
package com.itheima.test;public class Test2 { /* 需求:设计一个方法用于获取数组中元素的最大值 思路: 1.定义一个数组,用静态初始化完成数组元素初始化 2.定义一个方法,用来获取数组中的最大值 3.调用获取最大值方法,用变量接收返回结果 4.把结果输出在控制台 */ public static void main(String[] args) { // 1.定义一个数组,用静态初始化完成数组元素初始化 int[] arr = {11, 55, 22, 44, 33}; // 3.调用获取最大值方法,用变量接收返回结果 int max = getMax(arr); // 4.把结果输出在控制台 System.out.println(max); } /* 2.定义一个方法,用来获取数组中的最大值 1, 参数 int[] arr 2, 返回值类型 int */ public static int getMax(int[] arr){ int max = arr[0]; for (int i = 1; i < arr.length; i++) { if(max < arr[i]){ max = arr[i]; } } return max; }}
18方法同时获取数组最大值和最小值
package com.itheima.test;public class Test3 { /* 需求:设计一个方法,该方法能够同时获取数组的最大值,和最小值 注意: return语句, 只能带回一个结果. */ public static void main(String[] args) { int[] arr = {11,55,33,22,44}; int[] maxAndMin = getMaxAndMin(arr); System.out.println(maxAndMin[0]); System.out.println(maxAndMin[1]); } public static int[] getMaxAndMin(int[] arr){ int max = arr[0]; for (int i = 1; i < arr.length; i++) { if(max < arr[i]){ max = arr[i]; } } int min = arr[0]; for (int i = 1; i < arr.length; i++) { if(min > arr[i]){ min = arr[i]; } } int[] maxAndMin = {min, max}; return maxAndMin; }}