简体   繁体   中英

How Can I Make That Pattern in c#

Hello how can I make this pattern on 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 | 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 | is the number of character fields between the characters. This is the symbol that I want to have. Correct Pattern

Output from my problem

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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