c#program输入三角形
时间: 2024-05-07 08:22:44 浏览: 8
以下是 C# 中输入三角形的示例代码:
```csharp
using System;
public class Program {
public static void Main() {
Console.WriteLine("请输入三角形的三条边长:");
double a = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());
if (a + b > c && a + c > b && b + c > a) {
Console.WriteLine("这是一个合法的三角形。");
} else {
Console.WriteLine("这不是一个合法的三角形。");
}
}
}
```
在上面的示例代码中,我们使用 `Console.ReadLine()` 方法从用户处接收三角形的三条边长。接着,我们使用 `if` 条件语句判断这三条边能否构成一个合法的三角形。如果是,则输出“这是一个合法的三角形。”;否则,输出“这不是一个合法的三角形。”。
相关问题
c# 实现谢尔宾斯基三角形
以下是C#实现谢尔宾斯基三角形的代码示例:
```csharp
using System;
using System.Drawing;using System.Windows.Forms;
public class SierpinskiTriangle : Form
{
private Graphics g;
private Pen pen;
private int count;
public SierpinskiTriangle()
{
this.Text = "Sierpinski Triangle";
this.Size = new Size(600, 600);
this.BackColor = Color.White;
this.count = 6;
this.pen = new Pen(Color.Black, 1);
this.g = this.CreateGraphics();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawSierpinskiTriangle(0, this.ClientSize.Height - 50, this.ClientSize.Width, this.ClientSize.Height - 50, count);
}
private void DrawSierpinskiTriangle(int x1, int y1, int x2, int y2, int count)
{
if (count == 0)
{
Point[] points = { new Point(x1, y1), new Point(x2, y1), new Point((x1 + x2) / 2, y2) };
g.DrawPolygon(pen, points);
}
else
{
int xa = (2 * x1 + x2) / 3;
int xb = (x1 + 2 * x2) / 3;
int yb = (2 * y1 + y2) / 3;
int ya = yb;
int xc = (x1 + x2) / 2;
int yc = y1;
DrawSierpinskiTriangle(x1, y1, xa, ya, count - 1);
DrawSierpinskiTriangle(xa, ya, xc, yc, count - 1);
DrawSierpinskiTriangle(xc, yc, xb, yb, count - 1);
DrawSierpinskiTriangle(xb, yb, x2, y2, count - 1);
}
}
}
class Program
{
static void Main(string[] args)
{
Application.Run(new SierpinskiTriangle());
}
}
```
c#输出倒三角形星号
以下是使用C#输出倒三角形星号的代码:
```csharp
using System;
class Program {
static void Main(string[] args) {
int i, j;
for (i = 5; i >= 1; i--) {
for (j = 1; j <= 5 - i; j++) {
Console.Write(' ');
}
for (j = i; j >= 1; j--) {
Console.Write('*');
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)