控制台c#上的备用字符颜色

时间:2015-09-04 15:36:51

标签: c#

我正在尝试将一些C#字符从蓝色替换为白色。我在那里有if语句来检查数字是奇数还是偶数。这似乎不起作用。

static void Main(string[] args)
{
    // Ask for minutes.
    //Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("Minutes?");
    string minutes = Console.ReadLine();
    int mins = int.Parse(minutes);
    for (int i = 0; i < mins; i++)
    {
        // Sixty seconds is one minute.
        System.Threading.Thread.Sleep(1000 * 60);
        // Write line.
        if (i % 2 == 0)
        {
            //iseven
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(new string('&', 30));
        }
        else
        {
            //is odd
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(new string('*', 30));
        }

    }
    // Beep ten times.
    for (int i = 0; i < 10; i++)
    {
        Console.Beep(200,1000);
    }
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("[Done]");
    Console.Read();
}

5 个答案:

答案 0 :(得分:2)

两个语句似乎都将颜色设置为ConsoleColor.Blue。将其中一个更改为ConsoleColor.White

if (i % 2 == 0)
{
     //iseven
     Console.ForegroundColor = ConsoleColor.Blue;
     Console.WriteLine(new string('&', 30));
}
else
{
    //is odd
    Console.ForegroundColor = ConsoleColor.White; //<--- here.
    Console.WriteLine(new string('*', 30));
}

答案 1 :(得分:1)

I have reviewed your main function and it seems that your problem is that you are always setting the Foreground Color to Blue. This will work:

if (i % 2 == 0)
                {
                    //iseven
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(new string('&', 30));
                }
                else
                {
                    //is odd
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(new string('*', 30));
                }

As simple as changing the first statement in the else clause will achieve what you want.

答案 2 :(得分:0)

这是有效的

Console.WriteLine("Minutes?");
string minutes = Console.ReadLine();
int mins = int.Parse(minutes);
for (int i = 0; i < mins; i++)
{
    // Sixty seconds is one minute.
    System.Threading.Thread.Sleep(1000 * 1);
    // Write line.
    if (i % 2 == 0)
    {
        //iseven
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(new string('&', 30));
    }
    else
    {
        //is odd
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine(new string('*', 30));
    }

}
// Beep ten times.
for (int i = 0; i < 10; i++)
{
    Console.Beep(200, 1000);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("[Done]");
Console.Read();

答案 3 :(得分:0)

Dave Zych's answer是正确的,我想我只是展示了另一种方法。

for (int i = 0; i < 10; i++)
{
    // Removed sleep and other calls.
    Console.ForegroundColor = i % 2 == 0 ? ConsoleColor.Blue : ConsoleColor.White;
    Console.WriteLine(new string(i % 2 == 0 ? '&': '*', 30));
}

免费奖金方法:

ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Blue, ConsoleColor.White };
char[] messageChars = new char[] { '&', '*' };

for (int i = 0; i < 10; i++)
{
    Console.ForegroundColor = colors[i % 2];
    Console.WriteLine(new String(messageChars[i % 2], 30));
}

或者,如果你想真的疯了:

Func<int, ConsoleColor> color = i => i % 2 == 0 ? ConsoleColor.Blue : ConsoleColor.White;
Func<int, string> message = i => new String(i % 2 == 0 ? '&' : '*', 30);

for (int i = 0; i < 10; i++)
{
    Console.ForegroundColor = color(i);
    Console.WriteLine(message(i));
}

答案 4 :(得分:0)

戴夫和B也是正确的。但是你正在寻找这个答案,你正在尝试输入分钟= 1,结果你想要前景颜色像你在上面评论中提到的白色一样:

static void Main(string[] args)
{
   // Ask for minutes.
  //Console.ForegroundColor = ConsoleColor.White;
  Console.WriteLine("Minutes?");
  string minutes = Console.ReadLine();
  int mins = int.Parse(minutes);
  //for (int i = 0; i < mins; i++)
  for (int i = 1; i <= mins; i++)
  {
     // Sixty seconds is one minute.
     System.Threading.Thread.Sleep(1000 * 60);
     // Write line.
     if (i % 2 == 0)
     {
        //iseven
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(new string('&', 30));
     }
     else
     {
        //is odd
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine(new string('*', 30));
     }

  }
  // Beep ten times.
  for (int i = 0; i < 10; i++)
  {
     Console.Beep(200, 1000);
  }
  Console.ForegroundColor = ConsoleColor.White;
  Console.WriteLine("[Done]");
  Console.Read();
}

所以你需要用这个来更新你的第一个“for”语句: for(int i = 1; i&lt; = mins; i ++)

和用于奇数的ELSE语句: Console.ForegroundColor = ConsoleColor.White;

就是这样。您需要更新代码。您的代码将按预期结果运行。