C# 如何显示垂直直方图?
C# How to display vertical Histogram?
我是 C# 的新手,我正在尝试创建一个控制台应用程序来创建由星星组成的垂直和水平直方图。所以我要求用户输入 1-10 之间的 8 个数字,并将他们的结果作为直方图打印到屏幕上。
我需要垂直显示直方图的帮助,我已经完成了水平直方图,但不知道如何使其垂直。这是我到目前为止所得到的:
非常感谢任何帮助。先感谢您。
我希望它看起来像这样:
*
* *
* * *
* * * *
* * * * *(Height of row depends on numbers user enters.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class Program
{
static void Main(string[] args)
{
clsMainMenu MainMenu = new clsMainMenu();
ConsoleKeyInfo ConsoleKeyPressed;
do
{
MainMenu.DisplayMenu();
ConsoleKeyPressed = Console.ReadKey(false);
Console.WriteLine();
switch (ConsoleKeyPressed.KeyChar.ToString())
{
case "1":
clsHistogram Histogram = new clsHistogram();
Histogram.CreateHorizontalHistogram();
break;
case "2":
clsHistogram HistogramV = new clsHistogram();
HistogramV.CreateVerticalHistogram();
break;
}
} while (ConsoleKeyPressed.Key != ConsoleKey.Escape);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class clsMainMenu
{
public void DisplayMenu()
{
Console.WriteLine("1. Create a Horizontal Histogram.");
Console.WriteLine("2. Create a Vertical Histogram.");
Console.WriteLine("Press Esc to exit the Program.");
Console.WriteLine("..................................");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class clsHistogram
{
string strNumberChosen = "";
public void CreateHorizontalHistogram()
{
Console.WriteLine("Please enter a number between 1 and 10:");
int[] intHistogramArray = new int[8];
for (int intCounter = 0; intCounter < 8; intCounter++)
{
Console.WriteLine("Enter number " + (intCounter + 1) + " :");
strNumberChosen = Console.ReadLine(); // Need Data Validation Here.
} // Populating Array.
Console.WriteLine("Your Histogram looks like this: ");
for (int intcounter = 0; intcounter < 8; intcounter++)
{
int intStarPlot = intHistogramArray[intcounter];
while (intStarPlot > 0)
{
Console.Write(" *");
intStarPlot -= 1;
}
Console.WriteLine();
} // Display a Horizontal Array.
}
public void CreateVerticalHistogram()
{
Console.WriteLine("Please enter a number between 1 and 10:");
int[] intHistogramArray = new int[8];
for (int intCounter = 0; intCounter < 8; intCounter++)
{
Console.WriteLine("Enter number " + (intCounter + 1) + " :");
strNumberChosen = Console.ReadLine(); // Need Data Validation Here.
} // Populating Array.
Console.WriteLine("Your Histogram looks like this: ");
for (int intcounter = 0; intcounter < 8; intcounter++)
{
int intStarPlot = intHistogramArray[intcounter];
while (intStarPlot > 0)
{
Console.Write(" * \n");
intStarPlot -= 1;
}
Console.WriteLine();
} // Display a Vertical Array.
}
}
}
int currentValue = 1;
bool allDone = false;
Console.WriteLine("Your Histogram looks like this: ");
while (!(allDone))
{
int x = 0;
for (int intcounter = 0; intcounter < 8; intcounter++)
{
if (intHistogramArray[intcounter] >= currentValue)
{
Console.Write(" * ");
}
else
{
Console.Write(" ");
x = x + 1;
}
}
if (x>=8) { allDone = true; }
currentValue = currentValue + 1;
Console.WriteLine();
}
输出:
Your Histogram looks like this:
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
如果你想让它们底部对齐,你必须做一些细微的修改,这只是给你一个如何开始的想法。
这里有一个小测试程序,可以帮助您开始创建垂直直方图。请注意,我结合了我为水平直方图提供的最后一个解决方案并使代码更普遍适用:
private static readonly char star = '*';
private static readonly uint minValue = 1;
private static readonly int maxValue = 10;
static void Main(string[] args)
{
var list = GetHistorgramData();
CreateHorizontalHistogram(list);
CreateVerticalHistogram(list);
}
private static void CreateHorizontalHistogram(List<int> list)
{
Console.WriteLine("Your Horizontal Histogram looks like this: ");
//foreach(var value in list)
//{
// Console.WriteLine(string.Empty.PadRight(value, star));
//}
//Console.WriteLine("Or like this with LINQ");
list.ForEach(n => Console.WriteLine(string.Empty.PadRight(n, star)));
}
private static void CreateVerticalHistogram(List<int> list)
{
Console.WriteLine("Your Vertical Histogram looks like this: ");
for(int i = 0; i < maxValue + 1; i++)
{
var displayLine = string.Empty;
foreach(int x in list)
{
displayLine += ((x + i) - maxValue) > 0 ? star.ToString() : " ";
}
Console.WriteLine(displayLine);
}
}
private static List<int> GetHistorgramData()
{
var limits = "a number between " + minValue + " and " + maxValue + ": ";
Console.WriteLine("Please enter " + limits);
var list = new List<int>();
do
{
var message = string.Empty;
bool isNumber = false;
bool isRightSize = false;
int output;
do
{
var input = Console.ReadLine();
isNumber = int.TryParse(input, out output);
if(isNumber)
{
isRightSize = minValue <= output && output <= maxValue;
message = isRightSize ? "That will do: " : "Try again - value is not " + limits + output;
}
else
{
message = "Try again - " + input + " is not a Number";
}
Console.WriteLine(message);
}while(!isNumber || !isRightSize);
list.Add(output);
Console.WriteLine("Entered number at position" + list.Count + " : " + output);
}while(list.Count < 8);
return list;
}
垂直结果:
*
**
**
****
****
******
*******
********
********
输入:
Please enter a number between 1 and 10:
2
Entered number at position1 : 2
4
Entered number at position2 : 4
6
Entered number at position3 : 6
8
Entered number at position4 : 8
9
Entered number at position5 : 9
6
Entered number at position6 : 6
4
Entered number at position7 : 4
3
Entered number at position8 : 3
注意:
我建议您对垂直和水平都使用方法 GetHistorgramData()
。
您可以决定是否要对水平直方图使用 LINQ
或 foreach
循环版本。
我想我可以为垂直直方图制作 LINQ
版本,但我觉得这看起来可能令人困惑。
您可能想稍微调整直方图的样式,但请记住 space“”的宽度与星号“*”的宽度不同。
请让我知道,如果你有任何问题。
我是 C# 的新手,我正在尝试创建一个控制台应用程序来创建由星星组成的垂直和水平直方图。所以我要求用户输入 1-10 之间的 8 个数字,并将他们的结果作为直方图打印到屏幕上。 我需要垂直显示直方图的帮助,我已经完成了水平直方图,但不知道如何使其垂直。这是我到目前为止所得到的: 非常感谢任何帮助。先感谢您。 我希望它看起来像这样:
*
* *
* * *
* * * *
* * * * *(Height of row depends on numbers user enters.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class Program
{
static void Main(string[] args)
{
clsMainMenu MainMenu = new clsMainMenu();
ConsoleKeyInfo ConsoleKeyPressed;
do
{
MainMenu.DisplayMenu();
ConsoleKeyPressed = Console.ReadKey(false);
Console.WriteLine();
switch (ConsoleKeyPressed.KeyChar.ToString())
{
case "1":
clsHistogram Histogram = new clsHistogram();
Histogram.CreateHorizontalHistogram();
break;
case "2":
clsHistogram HistogramV = new clsHistogram();
HistogramV.CreateVerticalHistogram();
break;
}
} while (ConsoleKeyPressed.Key != ConsoleKey.Escape);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class clsMainMenu
{
public void DisplayMenu()
{
Console.WriteLine("1. Create a Horizontal Histogram.");
Console.WriteLine("2. Create a Vertical Histogram.");
Console.WriteLine("Press Esc to exit the Program.");
Console.WriteLine("..................................");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise_3A
{
class clsHistogram
{
string strNumberChosen = "";
public void CreateHorizontalHistogram()
{
Console.WriteLine("Please enter a number between 1 and 10:");
int[] intHistogramArray = new int[8];
for (int intCounter = 0; intCounter < 8; intCounter++)
{
Console.WriteLine("Enter number " + (intCounter + 1) + " :");
strNumberChosen = Console.ReadLine(); // Need Data Validation Here.
} // Populating Array.
Console.WriteLine("Your Histogram looks like this: ");
for (int intcounter = 0; intcounter < 8; intcounter++)
{
int intStarPlot = intHistogramArray[intcounter];
while (intStarPlot > 0)
{
Console.Write(" *");
intStarPlot -= 1;
}
Console.WriteLine();
} // Display a Horizontal Array.
}
public void CreateVerticalHistogram()
{
Console.WriteLine("Please enter a number between 1 and 10:");
int[] intHistogramArray = new int[8];
for (int intCounter = 0; intCounter < 8; intCounter++)
{
Console.WriteLine("Enter number " + (intCounter + 1) + " :");
strNumberChosen = Console.ReadLine(); // Need Data Validation Here.
} // Populating Array.
Console.WriteLine("Your Histogram looks like this: ");
for (int intcounter = 0; intcounter < 8; intcounter++)
{
int intStarPlot = intHistogramArray[intcounter];
while (intStarPlot > 0)
{
Console.Write(" * \n");
intStarPlot -= 1;
}
Console.WriteLine();
} // Display a Vertical Array.
}
}
}
int currentValue = 1;
bool allDone = false;
Console.WriteLine("Your Histogram looks like this: ");
while (!(allDone))
{
int x = 0;
for (int intcounter = 0; intcounter < 8; intcounter++)
{
if (intHistogramArray[intcounter] >= currentValue)
{
Console.Write(" * ");
}
else
{
Console.Write(" ");
x = x + 1;
}
}
if (x>=8) { allDone = true; }
currentValue = currentValue + 1;
Console.WriteLine();
}
输出:
Your Histogram looks like this:
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
如果你想让它们底部对齐,你必须做一些细微的修改,这只是给你一个如何开始的想法。
这里有一个小测试程序,可以帮助您开始创建垂直直方图。请注意,我结合了我为水平直方图提供的最后一个解决方案并使代码更普遍适用:
private static readonly char star = '*';
private static readonly uint minValue = 1;
private static readonly int maxValue = 10;
static void Main(string[] args)
{
var list = GetHistorgramData();
CreateHorizontalHistogram(list);
CreateVerticalHistogram(list);
}
private static void CreateHorizontalHistogram(List<int> list)
{
Console.WriteLine("Your Horizontal Histogram looks like this: ");
//foreach(var value in list)
//{
// Console.WriteLine(string.Empty.PadRight(value, star));
//}
//Console.WriteLine("Or like this with LINQ");
list.ForEach(n => Console.WriteLine(string.Empty.PadRight(n, star)));
}
private static void CreateVerticalHistogram(List<int> list)
{
Console.WriteLine("Your Vertical Histogram looks like this: ");
for(int i = 0; i < maxValue + 1; i++)
{
var displayLine = string.Empty;
foreach(int x in list)
{
displayLine += ((x + i) - maxValue) > 0 ? star.ToString() : " ";
}
Console.WriteLine(displayLine);
}
}
private static List<int> GetHistorgramData()
{
var limits = "a number between " + minValue + " and " + maxValue + ": ";
Console.WriteLine("Please enter " + limits);
var list = new List<int>();
do
{
var message = string.Empty;
bool isNumber = false;
bool isRightSize = false;
int output;
do
{
var input = Console.ReadLine();
isNumber = int.TryParse(input, out output);
if(isNumber)
{
isRightSize = minValue <= output && output <= maxValue;
message = isRightSize ? "That will do: " : "Try again - value is not " + limits + output;
}
else
{
message = "Try again - " + input + " is not a Number";
}
Console.WriteLine(message);
}while(!isNumber || !isRightSize);
list.Add(output);
Console.WriteLine("Entered number at position" + list.Count + " : " + output);
}while(list.Count < 8);
return list;
}
垂直结果:
*
**
**
****
****
******
*******
********
********
输入:
Please enter a number between 1 and 10:
2
Entered number at position1 : 2
4
Entered number at position2 : 4
6
Entered number at position3 : 6
8
Entered number at position4 : 8
9
Entered number at position5 : 9
6
Entered number at position6 : 6
4
Entered number at position7 : 4
3
Entered number at position8 : 3
注意:
我建议您对垂直和水平都使用方法 GetHistorgramData()
。
您可以决定是否要对水平直方图使用 LINQ
或 foreach
循环版本。
我想我可以为垂直直方图制作 LINQ
版本,但我觉得这看起来可能令人困惑。
您可能想稍微调整直方图的样式,但请记住 space“”的宽度与星号“*”的宽度不同。
请让我知道,如果你有任何问题。