01Java学习笔记
方法的定义和使用
一、方法的定义:
列:
package com.zxl;
public class FunctionTest {
public static double getPi(int r) {
float Pi = (float) 3.14;
float l = 2 * Pi * r;
return l;
}
public static void chang(double a, double b) {
System.out.println(a * b);
}
public static void getMax(float a, float b, float c) {
if (a > b && a > c) {
System.out.println(a);
} else if (b > a && b > c) {
System.out.println(b);
} else if (c > a && c > b) {
System.out.println(c);
}
}
public static void main(String[] args) {
getMax(12, 13, 14);
}
}
定义一个方法的格式:
标识符
返回值类型
方法名称
(参数数据类型 参数名称
){
函数体
;
return 返回值;
}
//这是一个返回长方形面积的方法
public static void chang(double width, double height) {
System.out.println(width* height);
}
二、函数的调用
public static void main(String[] args) {
//把定义好的函数在main方法中调用
chang(2,3);
}
注意:方法要写在类里面,不能写在main方法里面,但能在main方法中调用其他方法。