C# 如何在控制台应用程序中制作水平条形图

骑手乔恩

我需要制作一个水平条形图来表示一个数字在我的直方图字典中的出现。

但是,我尝试使用 Console.BackgroundColor 来处理,这显然只会使线条背景颜色变为蓝色。

static void Main(string[] args)
        {
            string Speach;
            Speach = "I say to you today, my friends, so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. " +
            "I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident: that all men are created equal. " +
            "I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood. " +
            "I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. " +
            "I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. " +
            "I have a dream today. I have a dream that one day, down in Alabama, with its vicious racists, with its governor having his lips dripping with the words of interposition and nullification; one day right there in Alabama, little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. " +
            "I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see it together. " +
            "This is our hope. This is the faith that I go back to the South with. With this faith we will be able to hew out of the mountain of despair a stone of hope. With this faith we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. " +
            "With this faith we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day. " +
            "This will be the day when all of God's children will be able to sing with a new meaning, My country, 'tis of thee, sweet land of liberty, of thee I sing. Land where my fathers died, land of the pilgrim's pride, from every mountainside, let freedom ring. " +
            "And if America is to be a great nation this must become true. So let freedom ring from the prodigious hilltops of New Hampshire. Let freedom ring from the mighty mountains of New York. Let freedom ring from the heightening Alleghenies of Pennsylvania! " +
            "Let freedom ring from the snowcapped Rockies of Colorado! Let freedom ring from the curvaceous slopes of California! But not only that; let freedom ring from Stone Mountain of Georgia! " +
            "Let freedom ring from Lookout Mountain of Tennessee! Let freedom ring from every hill and molehill of Mississippi. From every mountainside, let freedom ring. " +
            "And when this ha   ppens, when we allow freedom to ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God's children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual, Free at last! free at last! thank God Almighty, we are free at last!";

            Console.WriteLine("Histogram");
            Console.WriteLine();
            Console.WriteLine("1. Show Histogram");
            Console.WriteLine("2. Search for Word");
            Console.WriteLine("3. Exit");
            Console.WriteLine("Choice?");
            string choice = Console.ReadLine();
            int choiceInt = Int32.Parse(choice);

            string[] SpeachSplit = Speach.Split();
            Dictionary<string, int> Historgram = new Dictionary<string, int>();


            if (choiceInt == 1)
            {    
                    var result = Speach.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select(x => Regex.Replace(x.ToLower(), "[^a-zA-Z0-9-]", ""))
                                    .GroupBy(x => x)
                                    .ToDictionary(x => x.Key, x => x.Count());

                ConsoleColor BarColor;
                Console.BackgroundColor = ConsoleColor.Blue;
                BarColor = Console.BackgroundColor;

                foreach (var item in result.OrderByDescending(x => x.Value))
                        Console.WriteLine($"{item.Key} {BarColor}  {item.Value}");


            }
            else if(choiceInt == 2)
            {
                Console.WriteLine("Search word to find?");
                string wordSearch = Console.ReadLine();

                if (Historgram.TryGetValue(wordSearch, out int value))
                {
                    Console.WriteLine($"{wordSearch}, value = {0}.", wordSearch);
                }
                else
                {
                    Console.WriteLine($"{wordSearch} is not found.");
                }
            }
            else
            {
                Console.ReadLine();

            }
            Console.ReadLine();

我尝试查找并阅读许多关于如何执行此操作的文章,但它似乎非常复杂。我只是 C# 的初学者,所以我对如何制作这个单杠感到很困惑。最后,它看起来像这样:在此处输入图片说明

简化问题:如何制作一个水平条形图来表示直方图字典中某个数字的出现次数?

一般

那么你不会使用 a foreach,你会使用老式for 循环Write不是WriteLine

...

// print x amount of something
for(var i = 0; i < wordlength; i++)
   Console.Write("#"); 

// this creates the new line
Console.WriteLine(wordCount); 

这是假设它适合控制台。

这里还有其他问题,您有正确对齐的单词,并且您还需要学习如何在控制台中为文本着色。但是,我会将这些有趣的细节留给您。

更新

public static void Print(List<KeyValuePair<string,int>> list)
{

   // get the max length of all the words so we can align
   var max = list.Max(x => x.Key.Length);

   foreach (var item in list)
   {
      // right align using PadLeft and max length
      Console.Write(item.Key.PadLeft(max));

      Console.Write(" ");

      // change color
      Console.BackgroundColor = ConsoleColor.DarkBlue;

      // Write the bars
      for (var i = 0; i < item.Value; i++)
         Console.Write("#");

      // change back
      Console.BackgroundColor = ConsoleColor.Black;

      Console.Write(" ");
      // this creates the new line
      Console.WriteLine(item.Value);
   }
}

用法

var result = Speach.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                   .Select(x => Regex.Replace(x.ToLower(), "[^a-zA-Z0-9-]", ""))
                   .GroupBy(x => x)
                   .ToDictionary(x => x.Key, x => x.Count());

Print(result.OrderByDescending(x => x.Value).ToList());

输出

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在C#中的控制台应用程序中激活代码的不同部分

来自分类Dev

如何在C ++中从控制台应用程序显示MFC对话框?

来自分类Dev

如何在C#控制台应用程序中播放背景音乐

来自分类Dev

如何在C#控制台应用程序中返回简单的int值

来自分类Dev

如何在c#控制台应用程序中从.cshtml(razor)模板创建pdf

来自分类Dev

如何在控制台应用程序C#中打印值

来自分类Dev

如何在C#控制台应用程序中过滤文件类型,例如.exe

来自分类Dev

如何在c ++ / CLI应用程序中显示控制台?

来自分类Dev

如何在C ++中从控制台应用程序显示MFC对话框?

来自分类Dev

如何在控制台应用程序C#中访问SolidBrush

来自分类Dev

如何在ConEmu中运行C ++控制台程序(这是独立的C ++文件,而不是Code :: Blocks中的控制台应用程序项目)?

来自分类Dev

如何从C#中的控制台应用程序运行Web应用程序

来自分类Dev

在C#中的控制台应用程序中使用的信息如何在消息框中写入相同的信息

来自分类Dev

如何在.NET 5.0控制台应用程序上的C#中检查Windows版本

来自分类Dev

如果用户在C#程序中打印“ x”,如何使控制台应用程序退出?

来自分类Dev

如何在 C# 控制台应用程序中跟踪用户输入?

来自分类Dev

如何在后台运行 c# 控制台应用程序 .exe?

来自分类Dev

在C中按“ Ctrl + C”时如何防止控制台应用程序终止?

来自分类Dev

如何在Web应用程序ASP.net MVC中嵌入C#.net控制台应用程序

来自分类Dev

如何从C#控制台应用程序写入Azure WebJobs中的日志?

来自分类Dev

如何使用C#在内存中执行.NET控制台应用程序?

来自分类Dev

如何使用C#在内存中执行.NET控制台应用程序?

来自分类Dev

如何使用 C# 在控制台应用程序中执行命令或进程

来自分类Dev

如何跟踪 C++14 中的控制台应用程序已启动的次数?

来自分类Dev

如何在具有Windows应用程序输出类型的C#程序上显示控制台

来自分类Dev

如何在c3.js中在x轴上制作倒置条形图

来自分类Dev

如何在不覆盖当前数据的情况下将数据写入 C# 控制台应用程序中的文本文件

来自分类Dev

如何从提供程序(C#.NET Core控制台应用程序)获取Args?

来自分类Dev

C ++ GUI和控制台应用程序

Related 相关文章

  1. 1

    如何在C#中的控制台应用程序中激活代码的不同部分

  2. 2

    如何在C ++中从控制台应用程序显示MFC对话框?

  3. 3

    如何在C#控制台应用程序中播放背景音乐

  4. 4

    如何在C#控制台应用程序中返回简单的int值

  5. 5

    如何在c#控制台应用程序中从.cshtml(razor)模板创建pdf

  6. 6

    如何在控制台应用程序C#中打印值

  7. 7

    如何在C#控制台应用程序中过滤文件类型,例如.exe

  8. 8

    如何在c ++ / CLI应用程序中显示控制台?

  9. 9

    如何在C ++中从控制台应用程序显示MFC对话框?

  10. 10

    如何在控制台应用程序C#中访问SolidBrush

  11. 11

    如何在ConEmu中运行C ++控制台程序(这是独立的C ++文件,而不是Code :: Blocks中的控制台应用程序项目)?

  12. 12

    如何从C#中的控制台应用程序运行Web应用程序

  13. 13

    在C#中的控制台应用程序中使用的信息如何在消息框中写入相同的信息

  14. 14

    如何在.NET 5.0控制台应用程序上的C#中检查Windows版本

  15. 15

    如果用户在C#程序中打印“ x”,如何使控制台应用程序退出?

  16. 16

    如何在 C# 控制台应用程序中跟踪用户输入?

  17. 17

    如何在后台运行 c# 控制台应用程序 .exe?

  18. 18

    在C中按“ Ctrl + C”时如何防止控制台应用程序终止?

  19. 19

    如何在Web应用程序ASP.net MVC中嵌入C#.net控制台应用程序

  20. 20

    如何从C#控制台应用程序写入Azure WebJobs中的日志?

  21. 21

    如何使用C#在内存中执行.NET控制台应用程序?

  22. 22

    如何使用C#在内存中执行.NET控制台应用程序?

  23. 23

    如何使用 C# 在控制台应用程序中执行命令或进程

  24. 24

    如何跟踪 C++14 中的控制台应用程序已启动的次数?

  25. 25

    如何在具有Windows应用程序输出类型的C#程序上显示控制台

  26. 26

    如何在c3.js中在x轴上制作倒置条形图

  27. 27

    如何在不覆盖当前数据的情况下将数据写入 C# 控制台应用程序中的文本文件

  28. 28

    如何从提供程序(C#.NET Core控制台应用程序)获取Args?

  29. 29

    C ++ GUI和控制台应用程序

热门标签

归档