[英]How can I make a program generate a random number with a set pattern to follow in c# with .net framework?
[英]How Can I Make That Pattern in c#
你好,我怎样才能在 c# 上制作这个图案。 我只到了那么远。 但我不能做更多。 我找不到添加witdh的方法,我无法添加| 行尾的符号以及第二部分的开头和结尾。 问题:从键盘读取高度和宽度变量的数字并创建以下模式。 其中height是总行数宽度| 是字符之间的字符字段数。 这是我想要的符号。 正确的模式
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int i = 0, width, height;
Console.WriteLine("Width");
width = int.Parse(Console.ReadLine());
Console.WriteLine("Height");
height = int.Parse(Console.ReadLine());
for (i = 1; i <= width; i++)
{
Console.Write("|");
if (i > 1)
{
for (int k = 1; k < i; k++)
{
Console.Write(" ");
}
Console.Write("*");
for (int k = 1; k < i; k++)
{
Console.Write(" ");
}
}
else
{
Console.Write("*");
}
Console.WriteLine();
}
for (int a = i - 2; a >= 1; a--)
{
for (int k = 1; k < a; k++)
{
Console.Write(" ");
}
Console.Write("*");
Console.WriteLine();
}
Console.ReadLine();
}
}
}
尝试这个:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int width, height;
Console.WriteLine("Width");
width = int.Parse(Console.ReadLine());
Console.WriteLine("Height");
height = int.Parse(Console.ReadLine());
var row = 0;
var col = 0;
var nextSwitch = width - 1;
while (row < height)
{
PrintStar(col, width);
if (col < nextSwitch)
{
nextSwitch = width - 1;
col++;
}
else
{
nextSwitch = 1;
col--;
}
row++;
}
}
public static void PrintStar(int pos, int width)
{
Console.Write("|");
var i = 0;
while (i < pos)
{
Console.Write(" ");
i++;
}
Console.Write("*");
i++;
while (i < width)
{
Console.Write(" ");
i++;
}
Console.WriteLine("|");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.