Java实现求多个矩形、三角形、圆形和梯形的周长和面积

实现:由用户选择对哪个图形进行计算,由用户决定是否退出程序,界面友好、可读性强。

import java.util.Scanner;

class rectangle//矩形
{
    int long1;//长
    int wide1;//宽
    public void setLong1(int long1)
    {
        this.long1=long1;
    }
    public int getLong1()
    {
        return long1;
    }
    public void setWide1(int wide1)
    {
        this.wide1=wide1;
    }
    public int getWide1()
    {
        return wide1;
    }
    public int perimeter()//求矩形周长
    {
        int c=0;
        c=(long1+wide1)*2;
        return c;
    }
    public float area()//求矩形面积
    {
        float s=0;
        s=long1*wide1;
        return s;
    }
}
class circular//圆
{
    int r;//圆的半径
    public void setr(int r)
    {
        this.r=r;
    }
    public int getr()
    {
        return r;
    }
    public float perimeter()//圆的周长
    {
        float c;
        c=2*3.14f*r;
        return c;
    }
    public float area()//圆的面积
    {
        float s;
        s=3.14f*r*r;
        return s;
    }
}
class triangle//三角形
{
    int a,b,d;//三边长
    float h;//高
    public void seta(int a)
    {
        this.a=a;
    }
    public void setb(int b)
    {
        this.b=b;
    }
    public void setd(int d)
    {
        this.d=d;
    }
    public void seth(float h)
    {
        this.h=h;
    }
    public int geta()
    {
        return a;
    }
    public int getb()
    {
        return b;
    }
    public int getd()
    {
        return d;
    }
    public float geth()
    {
        return h;
    }
    public int perimeter()//三角形周长
    {
        int c=0;
        c=a+b+d;
        return c;
    }
    public float area()//三角形面积
    {
        float s=0;
        s=d*h/2;
        return s;
    }

}
class trapezoid//等腰梯形
{
    int d1,d2,h,y;//上底、下底、高、腰
    public void setd1(int d1)
    {
        this.d1=d1;
    }
    public int getd1()
    {
        return d1;
    }
    public void setd2(int d2)
    {
        this.d2=d2;
    }
    public int getd2()
    {
        return d2;
    }
    public void seth(int h)
    {
        this.h=h;
    }
    public int geth()
    {
        return h;
    }
    public void sety(int y)
    {
        this.y=y;
    }
    public int gety()
    {
        return y;
    }
    public int perimeter()//梯形周长
    {
        int c=0;
        c=d1+d2+y*2;
        return c;
    }
    public float area()//梯形面积
    {
        float s=0;
        s=(d1+d2)*h/2;
        return s;
    }
}
public class work2
{
    static Scanner in=new Scanner(System.in);
    static rectangle jx=new rectangle();
    static circular yx=new circular();
    static triangle sjx=new triangle();
    static trapezoid tx=new trapezoid();
    public static void main(String[] args) 
    {
        int n=1;
        while(n!=0)
        {
            System.out.println("\n0.退出程序;1.矩形;2.圆形;3.三角形;4.梯形;\n");
            System.out.println("请选择要计算的图形:\n");
            n=in.nextInt();
            switch(n)
            {
                case 1:
                    System.out.println("请输入矩形的长和宽:\n");
                    jx.setLong1(in.nextInt());
                    jx.setWide1(in.nextInt());
                    System.out.print("矩形周长为:"+jx.perimeter());
                    System.out.print("\n矩形面积为:"+jx.area());
                    break;

                case 2:
                    System.out.println("请输入圆形的半径:\n");
                    yx.setr(in.nextInt());
                    System.out.print("圆形周长为:"+yx.perimeter());
                    System.out.print("\n圆形面积为:"+yx.area());
                    break;
                case 3:
                    System.out.println("请输入三角形的两个边长:\n");
                    sjx.seta(in.nextInt());
                    sjx.setb(in.nextInt());
                    System.out.println("请输入三角形的底边长:\n");
                    sjx.setd(in.nextInt());
                    System.out.println("请输入三角形的高:\n");
                    sjx.seth(in.nextFloat());
                    System.out.print("三角形周长为:"+sjx.perimeter());
                    System.out.print("\n三角形面积为:"+sjx.area());
                    break;
                case 4:
                    System.out.println("请输入等腰梯形的上底、下底、腰长、高:\n");
                    tx.setd1(in.nextInt());
                    tx.setd2(in.nextInt());
                    tx.sety(in.nextInt());
                    tx.seth(in.nextInt());
                    System.out.print("梯形周长为:"+tx.perimeter());
                    System.out.print("\n梯形面积为:"+tx.area());
                    break;
                case 0:
                    System.out.println("退出程序!\n");
                    break;
                default:
                    System.out.println("输入有误!");
                    break;
            }
        }
    }
        
}

 代码简略,希望能帮到大家理解Java语言。

  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的面向对象编程思想,将矩形三角形圆形抽象成类,并在类中定义面积的方法。 以矩形为例,可以定义一个矩形类(Rectangle),其中包含矩形的长和宽属性,以及矩形面积的方法(getArea()): ``` public class Rectangle { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } public double getArea() { return length * width; } } ``` 同样的,可以定义一个三角形类(Triangle),其中包含三角形的底边和高属性,以及三角形面积的方法(getArea()): ``` public class Triangle { private double base; private double height; public Triangle(double base, double height) { this.base = base; this.height = height; } public double getArea() { return base * height / 2; } } ``` 还可以定义一个圆形类(Circle),其中包含圆形的半径属性,以及圆形面积的方法(getArea()): ``` public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } } ``` 在主程序中,可以根据用户的输入创建不同的矩形三角形圆形对象,并调用它们的getArea()方法面积: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请选择要计算的图形:"); System.out.println("1. 矩形"); System.out.println("2. 三角形"); System.out.println("3. 圆形"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("请输入矩形的长和宽:"); double length = scanner.nextDouble(); double width = scanner.nextDouble(); Rectangle rectangle = new Rectangle(length, width); System.out.println("矩形面积为:" + rectangle.getArea()); break; case 2: System.out.print("请输入三角形的底边和高:"); double base = scanner.nextDouble(); double height = scanner.nextDouble(); Triangle triangle = new Triangle(base, height); System.out.println("三角形面积为:" + triangle.getArea()); break; case 3: System.out.print("请输入圆形的半径:"); double radius = scanner.nextDouble(); Circle circle = new Circle(radius); System.out.println("圆形面积为:" + circle.getArea()); break; default: System.out.println("输入有误!"); break; } } } ``` 这样就可以实现一个简单的矩形三角形圆形面积的系统了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值