在C#中使用枚举 [英] using enumeration in C#
本文介绍了在C#中使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hiiiiiiiiiiiiiiii ...
我想在这个程序中用户输入颜色名称,编译器会显示颜色和颜色的代码,如输出:
你选择的颜色是蓝色,颜色代码是101010.
请给我一些建议我该怎么办
Hiiiiiiiiiiiiiiii...
I want in this program that the user enter the color name and the compiler shows the color and color's code like that output:
"The color you have chosen is blue and the color code is 101010."
please give me some suggesstions that how can i do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
color skyblue = color.blue;
int code = (int)Enum.Parse(typeof(color), skyblue.ToString());
if (code == 101010)
{
Console.WriteLine("The color you have chosen is " + skyblue + " and the color code is {0}.",code.ToString());
Console.ReadKey();
}
}
public enum color
{
blue = 101010,
black = 000000,
white = 111111
}
}
}
推荐答案
试试这个:
Try This:
static void Main(string[] args)
{
string c=Console.ReadLine();
color skyblue = (color)Enum.Parse(typeof(color), c);
int code = (int)Enum.Parse(typeof(color), skyblue.ToString());
Console.WriteLine("The color you have chosen is " + skyblue + " and the color code is {0}.",code.ToString());
Console.ReadKey();
}
这 [ ^ ]是一个很好的例子,你可以展示不同的颜色在控制台窗口中。
关键是使用Console.BackgroundColor =(ConsoleColor)Enum.Parse(type,name);
恰当。
This[^] is a good example where you can show different colors in a console window.
The key is to useConsole.BackgroundColor = (ConsoleColor)Enum.Parse(type, name);
appropriately.
不要使用枚举,请使用字典< color,>
代替
Dont use enums, useDictionary<color,>
instead
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
populateColors();
//get user input here inside variable code
if (code == 101010)
{
Console.WriteLine("The color you have chosen is " + colorMap[code] + " and the color code is {0}.",code.ToString());
Console.ReadKey();
}
}
Dictionary<int,> colorMap = new Dictionary<int,>();
void populateColors()
{
colorMap[101010] = Color.Blue; //add more here
}
}
}
注意:我还没有编译它。它只是伪代码。
NOTE: i have not compiled it. it is just pseudo code.
这篇关于在C#中使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文