如何在 C# 控制台应用程序中绘制方框、矩形

How to Draw Box,Rectangle in a C# Console application(如何在 C# 控制台应用程序中绘制方框、矩形)

本文介绍了如何在 C# 控制台应用程序中绘制方框、矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了两个相关的问题.

I ask for 2 related questions.

1-我们如何将输出(例如结果和消息)放入 c# 控制台应用程序的框内.

1-How we can Put outputs(such as Results and Messages) inside a box in a c# console application.

2-我们如何在 c# 控制台应用程序中绘制矩形.感谢您提供任何示例教程或建议

2-How we can draw rectangle in a c# console application.thank u for any sample tutorial or advice

推荐答案

假设你的意思是一个字符框,这就可以了.

Assuming you just meant a character box this will do it.

 private static void DrawABox( int x, int y, int width, int height,char Edge,string Message )
    {
        int LastIndex =0 ;
        Console.SetCursorPosition(x, y);
        for ( int h_i = 0; h_i <= height ; h_i++ )
        {
            if ( LastIndex != -1 )
            {
                int seaindex = (LastIndex + ( width - 1) );
                if(seaindex >= Message.Length -1 )
                    seaindex = Message.Length - 1;
                int newIndex = Message.LastIndexOf(' ',seaindex);
                if(newIndex == -1 )
                    newIndex = Message.Length - 1;
                string substr = Message.Substring(LastIndex, newIndex - LastIndex);
                LastIndex = newIndex;
                Console.SetCursorPosition(x + 1, y + h_i);
                Console.Write(substr);
            }
            for ( int w_i = 0; w_i <= width; w_i++ )
            {

                if ( h_i % height == 0 || w_i % width == 0 )
                {
                    Console.SetCursorPosition(x + w_i, y + h_i);
                    Console.Write(Edge);
                }


            }

        }

我编辑了代码以在其中添加一条消息.您将需要在边界条件上做更多的工作.例如,消息中没有空格,一个比方框长的词,但这应该足以让你开始.

I edited the code to put a message in their. You will need to do more work on the boundary conditions. Ex no space in the message a word that is longer then the box but this should be enough to get you started.

这篇关于如何在 C# 控制台应用程序中绘制方框、矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 C# 控制台应用程序中绘制方框、矩形

基础教程推荐