代碼
提交代碼
class MethodDemo3 {
// 定義帶參數(shù)無返回值方法
public void printArea(float a, float b) { // 此處的 a,b 為參數(shù)變量
float result = a * b;
System.out.println( "長方形面積為:" + result);
}
public static void main(String[] args) {
// 實例化MethodDemo3
MethodDemo3 methodDemo3 = new MethodDemo3();
// 初始化兩個變量
float width = 12.3f;
float height = 20f;
// 調(diào)用printArea方法,并將 width、height變量作為參數(shù)傳入
methodDemo3.printArea(width, height);
// 也可不提前初始化變量,直接傳入浮點型字面量作為參數(shù)。
methodDemo3.printArea(10.2f, 2.5f);
}
}
運行結(jié)果