简体   繁体   English

如何在 c# 中制作该图案

[英]How Can I Make That Pattern in c#

Hello how can I make this pattern on c#.你好,我怎样才能在 c# 上制作这个图案。 I only got that far.我只到了那么远。 But I couldn't do more.但我不能做更多。 I couldn't find a way to add witdh and I couldn't add |我找不到添加witdh的方法,我无法添加| symbol in end of the lines and beginnig and ends of second part.行尾的符号以及第二部分的开头和结尾。 Question: Read the numbers for the height and width variables from the keyboard and create the following pattern.问题:从键盘读取高度和宽度变量的数字并创建以下模式。 Where height is the total number of rows width |其中height是总行数宽度| is the number of character fields between the characters.是字符之间的字符字段数。 This is the symbol that I want to have.这是我想要的符号。 Correct Pattern正确的模式

Output from my problem Output 我的问题

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();
        }
    }
}

Try this:尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM