# 用于计算矩形区域的 Java 程序

> 原文： [https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-rectangle/](https://beginnersbook.com/2014/01/java-program-to-calculate-area-of-rectangle/)

在本教程中，我们将看到如何**计算矩形区域**。

**程序 1：**
用户将在程序执行期间提供长度和宽度值，并根据提供的值计算面积。

```java
/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate Area of rectangle
 */
import java.util.Scanner;
class AreaOfRectangle {
   public static void main (String[] args)
   {
	   Scanner scanner = new Scanner(System.in);
	   System.out.println("Enter the length of Rectangle:");
	   double length = scanner.nextDouble();
	   System.out.println("Enter the width of Rectangle:");
	   double width = scanner.nextDouble();
	   //Area = length*width;
	   double area = length*width;
	   System.out.println("Area of Rectangle is:"+area);
   }
}
```

输出：

```java
Enter the length of Rectangle:
2
Enter the width of Rectangle:
8
Area of Rectangle is:16.0
```

**程序 2：**
在上述程序中，将要求用户提供长度和宽度值。如果您不需要用户交互并且只想在程序中指定值，请参阅以下程序。

```java
/**
 * @author: BeginnersBook.com
 * @description: Calculation with no user interaction
 */
class AreaOfRectangle2 {
   public static void main (String[] args)
   {
	   double length = 4.5;
	   double width = 8.0;
	   double area = length*width;
	   System.out.println("Area of Rectangle is:"+area);
   }
}
```

Output:

```java
Area of Rectangle is:36.0
```