用Java实现计算矩形的面积与周长

 

一、需求分析

              输入长宽,计算出矩形面积与周长。

二、功能设计

             编写程序,定义一个矩形类,具有长、宽、面积、周长共四个成员变量,计算面积和计算周长的两个方法,在主类中创建矩形对象,输入长和宽,输出矩形的面积和周长。

三、测试运行

         

1、主方法:

2、两个方法:

四、代码

import java.util.*;

public class juxing {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int mianji, zhouchang;
M haha = new M();
System.out.print("输入长:");
haha.chang = in.nextInt();
System.out.print("输入宽:");
haha.kuan = in.nextInt();
System.out.println("输出面积:" + haha.getmianji());
System.out.print("输入周长:" + haha.getzhouchang());
}
}

class M {
int chang, kuan, zhouchang, mianji;

int getmianji() {
return chang * kuan;
}

int getzhouchang() {
return (chang + kuan) * 2;
}
}

         

转载于:https://www.cnblogs.com/wyd123/p/11537522.html

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现各种形状的面积周长计算的示例代码: 1. 计算圆形面积周长: ``` import java.util.Scanner; public class Circle { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the radius of the circle: "); double radius = input.nextDouble(); double area = Math.PI * radius * radius; double circumference = 2 * Math.PI * radius; System.out.println("Area of the circle = " + area); System.out.println("Circumference of the circle = " + circumference); } } ``` 2. 计算长方形面积周长: ``` import java.util.Scanner; public class Rectangle { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the length of the rectangle: "); double length = input.nextDouble(); System.out.print("Enter the width of the rectangle: "); double width = input.nextDouble(); double area = length * width; double perimeter = 2 * (length + width); System.out.println("Area of the rectangle = " + area); System.out.println("Perimeter of the rectangle = " + perimeter); } } ``` 3. 计算正方形面积周长: ``` import java.util.Scanner; public class Square { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the side of the square: "); double side = input.nextDouble(); double area = side * side; double perimeter = 4 * side; System.out.println("Area of the square = " + area); System.out.println("Perimeter of the square = " + perimeter); } } ``` 4. 计算三角形面积周长: ``` import java.util.Scanner; public class Triangle { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the base of the triangle: "); double base = input.nextDouble(); System.out.print("Enter the height of the triangle: "); double height = input.nextDouble(); System.out.print("Enter the first side of the triangle: "); double side1 = input.nextDouble(); System.out.print("Enter the second side of the triangle: "); double side2 = input.nextDouble(); double area = 0.5 * base * height; double perimeter = side1 + side2 + base; System.out.println("Area of the triangle = " + area); System.out.println("Perimeter of the triangle = " + perimeter); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值