设置“重要” - 消息红色,其他白色

时间:2015-11-05 09:56:34

标签: c# console-application textcolor

我正在制作日历,我希望将重要会议设置为红色,其他会议设置为白色。我怎样才能做到这一点?当我为最后一行设置红色时,不重要的会议也是红色的。我的代码:

string important;
Console.Write("High priority? input yes or no: ");
important = Console.ReadLine();

if (important == "yes" || important == "Yes")
{
    important = "Important";
}
else
{
    important = "Normal";
}

Console.Write("Priority: " + important);

3 个答案:

答案 0 :(得分:2)

如果您将ForeGroundColor更改为Red,则必须将其重置为Gray,这是默认颜色。您可以使用此代码

Console.Write("High priority? input yes or no: ");
string important = Console.ReadLine();

if (important.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
{
    Console.Write("Priority: ");
    Console.ForegroundColor = ConsoleColor.Red; 
    Console.Write("Important");        
}
else
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write("Priority: Normal");
}
Console.ResetColor(); //default

答案 1 :(得分:1)

使用Console.ForegroundColor像这样:

important = Console.ReadLine();

Console.Write("Priority: ");

if (important == "yes" || important == "Yes")
{
    Console.ForegroundColor = ConsoleColor.Red ;
    important = "Important";
}
else
{
    Console.ForegroundColor = ConsoleColor.White;
    important = "Normal";
}
Console.Write(important);

答案 2 :(得分:0)

检查Arghya C的回答。

旧代码:

string important;

      Console.Write("\n\nIs the meeting high priority?\n Input \"Yes\" or \"No\": ");

      important = Console.ReadLine();

if (important == "yes" || important == "Yes")
      {
        Console.Write("\nPriority: \t");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("Important");
      }
      else
      {
        Console.Write("\nPriority: \t");
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write("Normal");
      }