【问题标题】:Drawing the letter "M" in the console using C#使用 C# 在控制台中绘制字母“M”
【发布时间】:2019-05-16 15:14:31
【问题描述】:

我在控制台中使用“*”作为字母并使用“-”作为空格来绘制字母 M 时遇到了麻烦。

从控制台读取字母的粗细。

其中-n是一个正整数并且总是一个奇数 2

到目前为止,我还没有想出任何类似的解决方案。

int  n = int.Parse(Console.ReadLine());
for (int i = n+1; i > 0; i--)
        {
            for (int j = 1; j < i; j++)
            {
                Console.Write("-");
            }
            for (int j = 1; j <= n; j++)
            {
                Console.Write("*");
            }
            Console.WriteLine();
        }

示例:如果 n = 5,字母应该如下所示:

编辑:我为写得很糟糕的问题道歉,这是我第一次在这里问。 :(

【问题讨论】:

  • 请添加minimal reproducible example 来说明您想要实现的目标。我知道它应该做什么,但我不明白输入的作用。这是为了某种家庭作业吗?
  • 我猜,n 应该是行数吧?

标签: c# console


【解决方案1】:

查看下面的 .Net Fiddle 链接,这是任何字母或字母的通用解决方案,我将它分叉以处理字母,因为原作者将其设计为处理图像,尝试使用变量 M,100

https://dotnetfiddle.net/DEY9il

输出将类似于以下内容:-

Enter the letter?M
Enter Line Character Amount100

 ###################################################################################################
 ###################################################################################################
 ###################################################################################################
 ####  .#######*  ##################################################################################
 ####   @######   ##################################################################################
 ####   .#####* . ##################################################################################
 ####  + %#### +  ##################################################################################
 ####  # -###+ #  ##################################################################################
 ####  #: ### :#  ##################################################################################
 ####  ## .#w ##. ##################################################################################
 ####  ##: @ *##. ##################################################################################
 ####  ##@ , @##  ##################################################################################
 ####  ###, ,###. ##################################################################################
 ###################################################################################################
 ###################################################################################################
 ###################################################################################################
 ###################################################################################################
 ###################################################################################################

你可以调整它以只打印你想要的字符。

有一百万种方法可以做到这一点,但这是另一种使用函数的方法,将 M 分成四行,并为此绘制。

using System;
using System.Collections.Generic;                   
public class Program
{
    static List<List<string>> board;
    public static void Main()
    {
        Console.WriteLine("Enter Width:");
        var width=ReadWidth();
        var emptyText="-";
        var fillText="*";

        board=new List<List<string>>();

        //for M we will use 4 points each with a width of [width]
        Func<int,int,bool> p1=(row,col)=>{ 
                        int startX=width-1-row;
                        return (col>startX && col<=(startX+width));
                    };
        Func<int,int,bool> p2=(row,col)=>{ 
                        int startX=width-1+row;
                        return (col>startX && col<=(startX+width));
                    };
        Func<int,int,bool> p3=(row,col)=>{ 
                        int startX=(3*width)-1-row;
                        return (col>startX && col<=(startX+width));
                    };
        Func<int,int,bool> p4=(row,col)=>{ 
                        int startX=(3*width)-1+row;
                        return (col>startX && col<=(startX+width));
                    };



        for(int row=0;row<=width;row++)
        {
            var rowChars=new List<string>();
            for(int col=0;col<(width*5);col++)
                rowChars.Add((p1(row,col) || p2(row,col) || p3(row,col) || p4(row,col))?fillText:emptyText);
            board.Add(rowChars);
        }
        ShowOutput();
    }
    public static int ReadWidth()
    {
    Console.WriteLine("Enter Width (odd number beteween 2< Input < 1000):");
        var widthString=Console.ReadLine();
        int width=0;
        if(!int.TryParse(widthString,out width) || width<2 || width >1000)
        {
        Console.WriteLine("The value entered is not accepted, please try again.");
            ReadWidth();
        }
        return width;
    }
    public static void ShowOutput()
    {
        var sb=new System.Text.StringBuilder();
        foreach(List<string> row in board)
            sb.AppendLine(string.Join("",row.ToArray()));

        Console.Write(sb.ToString());
    }
}

输入10,输出如下:-

Enter Width:
Enter Width (odd number beteween 2< Input < 1000):
10
----------**********----------**********----------
---------************--------************---------
--------**************------**************--------
-------****************----****************-------
------******************--******************------
-----****************************************-----
----**********--******************--**********----
---**********----****************----**********---
--**********------**************------**********--
-**********--------************--------**********-
**********----------**********----------**********

【讨论】:

    【解决方案2】:

    你基本上必须画四条线。每一行都是一个看起来像x=y*A+B 的函数。 xy 在这里交换了位置以更有意义。您逐行编写,在此过程中递增y,并且对于每个y,您循环通过x,并且对于每个x,您应该确定它是否符合至少一个将(x,y)放在里面的条件四行之一。每条线的条件是x 大于或等于y*A+B 且小于或等于y*A+B+N,其中N 是粗细。在这种特殊情况下,A 将是 1 或 -1。如果(x,y) 至少满足四个条件之一,则写'*',否则写'-'。您必须为四行中的每一行计算出AB 的值。您还必须弄清楚x 循环的范围(它必须在某个点结束,当您应该移动到另一行时)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 2011-09-04
      相关资源
      最近更新 更多