方法
1. 方法概述
/*
方法的概述:
方法就是解决一类问题办法,
举例子:买火车票
方法1. 使用APP
方法2. 直接去火车站售票大厅
方法3. 直接代购闪送
方法4. 家里管家 坐直升飞机 火车站买
特点:方法自己不会自动执行,需要人去执行
方法(method): 可以完成某个特定功能的代码块,用来解决一类编码问题;
方法的优点 : 可以提高代码的复用性
*/
public class Demo01 {
public static void main(String[] args) {
}
}
2. 方法的定义和调用
2.1 方法定义的格式1
/*
方法的定义及调用:
格式1:
无参无返回值的方法格式
public static void 方法名(){
方法体;
}
调用:
方法名();
提示:java中所有程序执行的入口是main方法,所以说我们的要在main方法中才能执行
分析方法定义:
public static 使我们方法固定写法
void :表示方法没有返回值,也就是说方法如果没有返回值,可以使用void 修饰
方法名:符合标识符的命名规则,遵守小驼峰的命名规范
(): 是方法的标识,小括号中什么都不写,表示这个方法没有参数
*/
public class Demo02 {
public static void main(String[] args) {
print();
//调用方法
getSum();
}
//定义方法打印 一行HelloWorld字符串
public static void print() {
System.out.println("HelloWorld");
}
//定义方法计算1--100之间的数据
public static void getSum(){
//1.定义变量
int sum = 0;
//2.使用循环遍历获取1---100之间所有整数
for (int i = 1; i <= 100; i++) {
//3.累加求和
sum+=i;
}
//输出结果
System.out.println("1到100之间所有的数据和:"+sum);
}
}
2.2 方法定义的格式2
/
*
方法的定义及调用:
格式2: 有参数无返回值方法的定义
方法中的参数,在方法名后面的小括号中定义 ,参数定义格式 :数据类型 变量名
public static void 方法名(参数列表){
方法体
}
参数列表:1.参数可以有一个,
格式:数据类型 变量名
2.参数还可以有多个,要使用逗号隔开
格式:数据类型 变量名,数据类型 变量名,数据类型 变量名......
方法体:就是完成题目需求
方法的调用:
方法名(参数);
注意:1.必须要传递参数
2.在调用方法的时候传递的数据的数据类要和方法中参数的数据类型要一致,隐含了一个自动类型转换
*/
public class Demo03 {
public static void main(String[] args) {
//调用方法
isEvent('a');
isEvent(10);
//方法的优点 : 可以提高代码的复用性
//准备数组
int[] arr = {72, 89, 65, 87, 91, 82, 71, 93, 76, 68};
//调用方法
printArray(arr);
//准备数组
int[] brr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
printArray(brr);
}
//定义一个方法判断任意整数,是奇数还是偶数
public static void isEvent(int num) {
if (num % 2 == 0) {
System.out.println(num + "是偶数");
} else {
System.out.println(num + "是奇数");
}
}
//定义方法格式输出任意整数数组中的数据,[72, 89, 65, 87, 91, 82, 71, 93, 76, 68]
public static void printArray(int[] array) {
System.out.print("[");
for (int i = 0; i < array.length; i++) {
//判断是否是最后一个索引
if (i == array.length - 1) {
System.out.print(array[i]);
} else {
System.out.print(array[i] + ", ");
}
}
System.out.println("]");
}
}
2.3 方法的优点
/*
方法的优点 : 可以提高代码的复用性
1.同一个类中,直接调用
2.同一个包中,不同的类,使用类名.方法名调用
3.不同包中
3.1 先导包
3.2 使用类名.方法名调用
*/
public class Demo04 {
public static void main(String[] args) {
int[] array = {11, 22, 33, 44, 55, 66, 77, 88, 99, 100};
//调用方法
Demo03.printArray(array);
}
}
package com.itheima._03方法优点;
import com.itheima._02方法.Demo03;
public class Demo01 {
public static void main(String[] args) {
int[] scores = {100, 99, 98, 97, 99, 100, 100};
Demo03.printArray(scores);
}
}
2.4 方法定义格式3
/*
方法定义的格式3:
无参有返回值方法定义和调用:
public staitc 返回值类型 方法名(){
方法体;
return 值;
}
返回值类型: 要和方法的返回值的数据类型一致
return:是方法要返回值的关键字,返回的值是方法体的最终结果
调用方法:
1.直接调用没报错 但是没有意义
2.赋值调用:定义变量,接收返回来的值,定义变量的数据类型和方法的返回值类型要一致
3.输出调用
*/
public class Demo05 {
public static void main(String[] args) {
// getSum(); 直接调用没报错 但是没有意义
//赋值调用
int s = getSum();
System.out.println("s = "+s);
//输出调用
System.out.println(getSum());
}
//定义方法 求1--100之间数据和,并返回给调用者
public static int getSum(){
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum+=i;
}
return sum;
}
}
2.5 方法定义的格式4
/*
方法定义的格式4:
有参有返回值方法的定义:
格式:
public static 返回值类型 方法名(参数列表){
方法体;
return 值;
}
调用:
1.直接调用 无意义不推荐使用
2.赋值调用
3.输出调用
*/
public class Demo06 {
public static void main(String[] args) {
//1.直接调用
//getSum(1, 1);
//2.赋值调用
int sum = getSum(1,1);
System.out.println(sum);
//3.输出调用
System.out.println(getSum(1, 1));
}
//定义方法求两个整数的和并返回 int类型
public static int getSum(int a, int b) {
int c = a + b;
return c;
}
}
2.6 方法定义的通用格式
/*
方法定义的通用格式:
public static 返回值类型 方法名(参数列表){
方法体;
return 值;
}
返回值类型:
1.如果方法没有返回值 返回值类型使用 void
2.如果方法有返回值,返回值类型要和return语句返回的值的数据类型一致
方法名:
1.方法名的定义遵守标识符的命名规则,符合小驼峰命名规范
2.方法名的命名做到见名知意
参数列表:
1.如果方法没有参数,参数列表为空
2.如果方法有参数,参数可以有一个,也可以有多个,当参数有多个时候,我们要使用逗号隔开
方法体:
方法体使用来完成需求的业务逻辑
return语句:
1.如果方法没有返回值,return可以省略不写,如果一定要写,只能写return;
2.如果方法有返回值,return语句后面值,一定要和方法的返回值类型一致
3.return语句表示方法的结束,return语句后面,不能再写代码,属于无效代码,永远都不会执行,还会报错
方法调用:
无返回值的方法
1.无参无返回值的方法
只能直接调用:方法名();
2.有参无返回值的方法
只能直接调用:方法名(参数列表);
有返回值的方法:
1.无参有返回值的方法
只能直接调用:方法名();没有意义
赋值调用
输出调用
2.有参有返回值方法调用
只能直接调用:方法名(参数列表);没有意义
赋值调用
输出调用
*/
public class Demo07 {
public static void main(String[] args) {
}
public static void add(){
System.out.println(1+1);
return;
}
public static int getResult(){
int c = 1+1;
return c;
//int d = 1+2;//Unreachable statement
}
}
3. 常见题型
3.1 习题1
/*
需求:设计一个方法(print) 用于打印 n 到 m 之间所有的奇数
分析:
public static void print(int n,int m){
// 方法体
遍历n到m之间所有数字,判断是否是奇数,如果则打印
}
*/
public class Demo01 {
public static void main(String[] args) {
print(10,20);
}
public static void print(int n,int m){
if (n>m){
System.out.println("第一个参数不能大于第二个参数");
return;
}
for (int i = n; i <= m; i++) {
//判断是否是奇数,如果则打印
if (i%2!=0){
System.out.println(i);
}
}
}
}
3.2 习题2
/*
需求:设计一个方法可以获取两个数的较大值,数据来自于参数
分析:
1.返回值类型 int
2.方法名
getMax
3.参数列表
int a, int b
*/
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个整数:");
int num2 = sc.nextInt();
//调用方法
int max = getMax(num1,num2);
System.out.println("max = "+max);
}
public static int getMax(int a,int b){
if (a>b){
return a;
}else {
return b;
}
}
}
4. 方法在参数传递
4.1 方法定义的注意事项
/*
方法定义的注意事项:
方法不能嵌套定义
方法的返回值类型为void,表示该方法没有返回值,没有返回值的方法可以省略return语句不写
如果要编写return,后面不能跟具体的数据。return语句下面,不能编写代码,因为永远执行不到,属于无效的代码
*/
public class Demo01 {
public static void main(String[] args) {
}
public static void add(){
}
}
4.2 形参和实参
/*
形参和实参
形参:形式参数是方法定义时的参数
实参:方法调用时传递的参数
*/
public class Demo02 {
public static void main(String[] args) {
//调用方法
add(10, 20);
System.out.println("----------");
//调用方法
int x = 30;
int y = 40;
add(x, y);
}
public static void add(int a, int b) {
System.out.println(a);
System.out.println(b);
System.out.println(a + b);
}
}
4.3 基本数据类型参数传递
/*
方法是在方法栈中执行:
栈的算法规则:先进后出;
基本数据类型在方参数传递过程中,形参的改变,不影响实参
*/
public class Demo03 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a); // 10
System.out.println(b); // 20
changeNum(a,b);
System.out.println(a); // 10
System.out.println(b); // 20
}
public static void changeNum(int a, int b) {
System.out.println(a); // 10
System.out.println(b); // 20
a = a+b; // 30
b = a+b; // 50
System.out.println(a); // 30
System.out.println(b); // 50
}
}
4.4 引用数据类型参数传递
/*
引用数据类型参数传递的过程中,形参的改变影响实参,
引用数据类型在方法调用过程中,传递的是地址值
*/
public class Demo04 {
public static void main(String[] args) {
int [] arr = new int[2];
System.out.println(arr);
System.out.println(arr[0]); //0
System.out.println(arr[1]); //0
changeNum(arr);
System.out.println(arr);
System.out.println(arr[0]); //??
System.out.println(arr[1]); //??
}
public static void changeNum(int[] arr) {
System.out.println(arr);
System.out.println(arr[0]); //0
System.out.println(arr[1]); //0
arr[0] = 11;
arr[1] = 22;
System.out.println(arr);
System.out.println(arr[0]); //11
System.out.println(arr[1]); //22
}
}
5.方法的重载
import java.util.Arrays;
/*
方法的重载:
A. 在同一个类中方法名相同
B. 参数类列表不同:
1.参数的数据类型不同 或者
2.参数的个数不同 或者
3.参数类型的顺序不一样
C. 和返回值类型无关
优点:
简化方法调用,调用更方便
*/
public class Demo01 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
//打印数组中的元素
System.out.println(Arrays.toString(arr));
char[] brr = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
//打印数组中的元素
System.out.println(Arrays.toString(brr));
double[] crr = {1.8, 2.8, 8.8, 1.88, 5.20, 13.14};
//打印数组中的元素
System.out.println(Arrays.toString(crr));
}
public static void method(int a, int b,int c,double d) {
}
public static void add() {
}
public static void add(int a) {
}
public static void add(double a) {
}
public static void add(int a, int b) {
}
public static void add(double a, int b) {
}
public static void add(int a, double b) {
}
/*public static double add(int a, double b) {
return a+b;
}*/
}
6.生成区间内的随机数
import java.util.Random;
/*
中随机数范围为 20 到 80(包含 20 和 80)
公式:
random.nextInt(最大值-最小值+1)+最小值;
*/
public class Demo02 {
public static void main(String[] args) {
Random random = new Random();
int num = random.nextInt(10);//0--9
int num1 = random.nextInt(61)+20;//20
//22 --99
int num2 = random.nextInt(78)+22;
random.nextInt(10); // 0--9
}
}