方法入门(java)

?完成某个功能的代码块
■如何定义方法
完整格式:权限修饰符 返回值类型 方法名(参数列表){
方法体
返回值
}
权限修饰符:固定 public static
返回值类型:目前不需要,void(无返回值)
方法名:自己定义的标识符(小驼峰)
参数列表:目前不写,但小括号必须有
方法体:写完成功能的代码
返回值:返回值类型为void,就不需要写返回值

public static void 方法名(){
    方法体
}

注意:
方法定义的位置:类中,其他方法外
方法的调用:一般我们再main方法中调用
数量:一个类中可以定义无数个方法

方法练习:

public class Test{
    public static void main(String[] args){
        maxNum();//调用方法
    }
    public static void maxNum(){
    int a=10;
    int b=20;
    int max=a>b?a:b;//刚上一篇提到的三木运算符
    System.out.println(max);
    }

}

猜你喜欢

转载自blog.csdn.net/YourMilkTea/article/details/81746919