java计算矩形面积跟周长
时间: 2023-06-10 14:07:44 浏览: 81
以下是Java代码示例,可以计算矩形的面积和周长:
```java
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
int length, width;
Scanner scanner = new Scanner(System.in);
System.out.print("请输入矩形的长度:");
length = scanner.nextInt();
System.out.print("请输入矩形的宽度:");
width = scanner.nextInt();
// 计算面积
int area = length * width;
System.out.println("矩形的面积为:" + area);
// 计算周长
int perimeter = 2 * (length + width);
System.out.println("矩形的周长为:" + perimeter);
}
}
```
在该示例中,我们使用了 `Scanner` 类来获取用户输入的矩形长度和宽度,然后使用长度和宽度计算矩形的面积和周长。
相关问题
java计算矩形面积跟周长类
以下是一个简单的 Java 代码实现,用于计算矩形的面积和周长:
```java
public class Rectangle {
double length;
double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getPerimeter() {
return 2 * (length + width);
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 10);
System.out.println("矩形的面积为:" + rectangle.getArea());
System.out.println("矩形的周长为:" + rectangle.getPerimeter());
}
}
```
在上述代码中,我们首先定义了一个 `Rectangle` 类,包含了矩形的长和宽两个属性,以及计算矩形面积和周长的方法。在 `main` 方法中,我们创建了一个具体的矩形对象,并通过调用 `getArea` 和 `getPerimeter` 方法分别计算了矩形的面积和周长,并将结果打印输出。
JAVA求圆和矩形面积周长
很抱歉,我不太清楚您的问题。我可以向您提供有关计算圆和矩形面积周长的JAVA代码:
计算圆的面积:
double radius = 5.0;
double area = Math.PI * radius * radius;
计算圆的周长:
double radius = 5.0;
double circumference = 2 * Math.PI * radius;
计算矩形的面积:
int width = 10;
int height = 20;
int area = width * height;
计算矩形的周长:
int width = 10;
int height = 20;
int circumference = 2 * (width + height);
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)