代碼
提交代碼
class MethodDemo3 {
// 定義帶參數無返回值方法
public void printArea(float a, float b) { // 此處的 a,b 為參數變量
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;
// 調用printArea方法,并將 width、height變量作為參數傳入
methodDemo3.printArea(width, height);
// 也可不提前初始化變量,直接傳入浮點型字面量作為參數。
methodDemo3.printArea(10.2f, 2.5f);
}
}
運行結果