Write the following code in c #, Write a method call CalculateAreaOfTriangle(), that prompts the user for the base and height of a triangle and then calculate and display the area.[ A=bt/2] Call the CalculateAreaOfTriangle() method twice from the same Main() method as in question 1.
时间: 2024-01-25 16:02:14 浏览: 85
C#程序求解三角形的面积
3星 · 编辑精心推荐
Sure, here's the code in C#:
```csharp
using System;
class Program
{
static void Main()
{
// Call the CalculateAreaOfTriangle method twice
CalculateAreaOfTriangle();
CalculateAreaOfTriangle();
}
static void CalculateAreaOfTriangle()
{
Console.Write("Enter the base of a triangle: ");
double b = double.Parse(Console.ReadLine());
Console.Write("Enter the height of a triangle: ");
double h = double.Parse(Console.ReadLine());
double area = b * h / 2;
Console.WriteLine($"The area of the triangle is: {area}");
}
}
```
This code defines a `Program` class with two methods: `Main` and `CalculateAreaOfTriangle`. In the `Main` method, we call `CalculateAreaOfTriangle` twice.
The `CalculateAreaOfTriangle` method prompts the user for the base and height of a triangle, reads in the input using `Console.ReadLine`, calculates the area using the formula `A = bt/2`, and then displays the result using `Console.WriteLine`.
Note that the `b` and `h` variables represent the base and height of the triangle, respectively, and that the `area` variable stores the calculated area of the triangle.
阅读全文