返回顶部

一缕半夏微光

温柔半两,从容一生

导航

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

参考链接:https://blog.csdn.net/lady_killer9/article/details/78207192 

一、效果如下:

二、代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //变量
14             int i;
15             double t_1, t_2, t_3;
16             double r_length, r_width;
17             double c, s;//c为周长,s为面积
18 
19             Console.WriteLine("-----欢迎进入几何图形周长、面积计算系统-----");
20             while (true)
21             {
22                 Console.WriteLine("1.三角形");
23                 Console.WriteLine("2.长方形");
24                 Console.WriteLine("3.退出");
25                 Console.WriteLine("请选择:");
26                 i = int.Parse(Console.ReadLine());
27                 switch (i)
28                 {
29                     case 1://三角形
30                         Console.WriteLine("请输入第一条边长:");
31                         t_1 = float.Parse(Console.ReadLine());
32                         Console.WriteLine("请输入第二条边长:");
33                         t_2 = float.Parse(Console.ReadLine());
34                         Console.WriteLine("请输入第三条边长:");
35                         t_3 = float.Parse(Console.ReadLine());
36                         //计算三角形的周长
37                         c = t_1 + t_2 + t_3;
38                         //计算三角形的面积
39                         s = Math.Sqrt(c / 2 * (c / 2 - t_1) * (c / 2 - t_2) * (c / 2 - t_3));
40                         //输出
41                         Console.WriteLine("该三角形的周长为:{0}", c);
42                         Console.WriteLine("该三角形的面积为:{0}", s);
43                         break;
44                 case 2:
45                         Console.WriteLine("请输入长方形的长:");
46                         r_length = float.Parse(Console.ReadLine());
47                         Console.WriteLine("请输入长方形的款:");
48                         r_width = float.Parse(Console.ReadLine());
49                         //计算长方形的周长
50                         c = 2 * (r_length + r_width);
51                         //计算长方形的面积
52                         s = r_length * r_width;
53                         //输出
54                         Console.WriteLine("该长方形的周长为:{0}", c);
55                         Console.WriteLine("该长方形的面积为:{0}", s);
56                         break;
57                     case 3:
58                         Console.WriteLine("已退出!");
59                         break;
60                     default:
61                         Console.WriteLine("输入错误!");
62                         break;
63                 }
64                 if (i == 3)
65                     break;//判断用户是否退出系统
66             }
67         }
68     }
69 }

posted on 2021-10-11 17:39  一缕半夏微光  阅读(223)  评论(0编辑  收藏  举报