查詢矩形面積的 Java 程式


矩形的面積是其長度和寬度的乘積。因此,要計算一個矩形的面積

  • 從使用者獲取矩形的長度。
  • 從使用者獲取矩形的寬度。
  • 計算它們的乘積。
  • 列印乘積。

示例

以下是使用 Scanner 類 在 Java 中查詢矩形面積的示例。

import java.util.Scanner;
public class AreaOfRectangle {
   public static void main(String args[]){
      int length, breadth, area;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the length of the rectangle ::");
      length = sc.nextInt();
      System.out.println("Enter the breadth of the rectangle ::");
      breadth = sc.nextInt();
      area = length* breadth;
      System.out.println("Area of the rectangle is ::"+area);
   }
}

輸出

Enter the length of the rectangle ::
56
Enter the breadth of the rectangle ::
48
Area of the rectangle is ::2688

更新日期: 18-Jun-2024

12K+ 瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.