每日博客

C#

编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。

using System;

namespace test

{

    class test_1

    {

        public void test_1_1()

        {

            int i=0;

            double a,b,c;

            double l,d;

            double C,S;

            while(i!=3)

            {

                Console.WriteLine("请选择:1.三角形   2.长方形   3.退出");

                i=int.Parse(Console.ReadLine());

                switch (i)

                {

                    case 1:

                        Console.WriteLine("请输入第一条边长");

                        a=float.Parse(Console.ReadLine());

                        Console.WriteLine("请输入第二条边长");

                        b=float.Parse(Console.ReadLine());

                        Console.WriteLine("请输入第三条边长");

                        c=float.Parse(Console.ReadLine());

                        C=a+b+c;

                        S=Math.Sqrt(C/2*(C/2-a)*(C/2-b)*(C/2-c));

                        Console.WriteLine("该三角形周长为:{0}",C);

                        Console.WriteLine("该三角形面积为:{0}",S);

                        break;

                    case 2:

                        Console.WriteLine("请输入长方形的长");

                        l=float.Parse(Console.ReadLine());

                        Console.WriteLine("请输入长方形的宽");

                        d=float.Parse(Console.ReadLine());

                        C=2*(d+l);

                        S=l*d;

                        Console.WriteLine("长方形的周长为:{0}",C);

                        Console.WriteLine("长方形的面积为:{0}",S);

                        break;

                    case 3:

                        Console.WriteLine("退出");

                        break;

                    default:

                        Console.WriteLine("输入错误");

                        break;

                }

            }

        }

    }

    class main

    {

        static void Main(string[] args)

        {

            test_1 t = new test_1();

            t.test_1_1();

        }

    }

}

 

posted @ 2021-10-02 21:01  谦寻  阅读(50)  评论(0编辑  收藏  举报