-3

i am wondering how to combine code + int/string

example.

string USERINPUT = Console.ReadLine();
Console.ForgroundColor = ConsoleColor.USERINPUT

but that does not work. how to i wonder?

4

1 回答 1

2

对于作业

Console.ForegroundColor = (something here);

您必须分配一个ConsoleColor,它是一个枚举。

您可以从其等效字符串中解析枚举值。

Console.ForegroundColor = 
    (ConsoleColor)System.Enum.Parse(typeof(ConsoleColor), USERINPUT);

详情见:

在 Enum 中搜索字符串并返回 Enum

请注意,我的代码不包括错误处理。如果用户在控制台输入不是 成员的字符串ConsoleColor,您将收到错误情况。

于 2013-03-25T20:26:02.857 回答