方法
完成特定功能的代码块
格式:
修饰符 返回值类型 方法名(参数类型 参数1,参数类型 参数名2){
方法体
return 返回值;
}
修饰符:public static
返回值类型: 限定返回值的类型
方法名: 程序调用的名称
参数名: 用于限定调用方法是传入的数据的类型
方法体: 完成特定的代码
return: 结束方法,返回值给调用者
注意事项:
方法和方法是平行关系
简单实例:
public class Test2 {
public static void main(String[] args) {
//第一种调用方式
int result=sum(1,2);
System.out.println(result);
//第二种调用方式
System.out.println(sum(3,4));
}
public static int sum (int a,int b){
int c=a+b;
return c;
}
}
方法调用图解
有返回值的方法
获取两个数的最大值
public class Test2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a= sc.nextInt();
int b= sc.nextInt();
int max=getMax(a