编写控制台应用程序(C#),实现漏斗图形的输出。谢谢啦,谢谢~~
发布网友
发布时间:2022-05-12 07:48
我来回答
共2个回答
热心网友
时间:2024-02-19 23:12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入漏斗的行数:");
int n = Convert.ToInt32(Console.ReadLine());
string[,] Array = new string[2 * n - 1, 2 * n - 1];
for (int i = 0; i < 2 * n - 1; i++)
{
for (int j = 0; j < 2 * n - 1; j++)
{
Array[i, j] = " ";
}
}
for (int i = 0; i < 2 * n - 1; i++)
{
for (int j = 0; j < 2 * n - 1; j++)
{
if ((i <= n - 1 && j >= i && j <= 2 * n - 2 - i) || (i > n - 1 && j <= i && j >= 2 * n - 2 - i))
{
if (i % 2 == 0 && j % 2 == 0)
{
Array[i, j] = "*";
}
if (i % 2 == 1 && j % 2 == 1)
{
Array[i, j] = "*";
}
}
}
}
for (int i = 0; i < 2 * n - 1; i++)
{
for (int j = 0; j < 2 * n - 1; j++)
{
Console.Write(Array[i, j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
热心网友
时间:2024-02-19 23:12
static void Main(string[] args)
{
Console.Write("请输入整数数字:");
string strNum = Console.ReadLine();
int num = 0;
if ( ! int.TryParse(strNum,out num))
{
Console.Write("输入整数呀!");
}
if (num > 0)
{
int middle = num;
for (int i = 0; i < 2 * num - 1; i++)
{
for (int h = 0; h < num - middle; h++)
{
Console.Write(" ");
}
if (i < num-1)
{
for (int j = 0; j < middle; j++)
{
Console.Write("*");
Console.Write(" ");
}
middle--;
}
else
{
for (int m = 0; m < middle; m++)
{
Console.Write("*");
Console.Write(" ");
}
middle++;
}
Console.Write("\n");
}
Console.ReadLine();
}
else
{
Console.Write("请输入大于0的数!");
}
Console.Read();
}