2013-02-15 31 views
2

假設你想用C#編寫一個程序並用命令提示符編譯它。假設簡單的程序只說;是否可以使命令輸出提示不同的顏色? C#編程

Console.WriteLine("a" + "b" + "c" + "d"); 

是否可以在命令提示符下以不同顏色打印a,b,c,d?

+2

http://stackoverflow.com/questions/7937256/changing-text-color-in-c-sharp-console-application但是,你需要改變顏色和WriteLine後寫各「d」。 – kenny 2013-02-15 22:06:30

回答

2

是的。你必須改變顏色和打印​​的文字,如:

Console.ForegroundColor = ConsoleColor.White; 
Console.WriteLine("a"); 
Console.ForegroundColor = ConsoleColor.Red; 
Console.WriteLine("b"); 
. 
... 
0

Console類具有ForegroundColor屬性,您可以使用:

Console.ForegroundColor = ConsoleColor.Blue; 
Console.Write(a); 
Console.ForegroundColor = ConsoleColor.Red; 
Console.Write(b); 
Console.ForegroundColor = ConsoleColor.Green; 
Console.Write(c); 
Console.ForegroundColor = ConsoleColor.White; 
Console.WriteLine(d); 
1

使用Console.ForegroundColor財產與ConsoleColor枚舉。

獲取或設置控制檯的前景色。

贊;

public static void Main(string[] args) 
    { 
     Console.ForegroundColor = ConsoleColor.White; 
     string a = "a"; 
     Console.WriteLine(a); 
     Console.ForegroundColor = ConsoleColor.Blue; 
     string b = "b"; 
     Console.WriteLine(b); 
     Console.ForegroundColor = ConsoleColor.DarkGreen; 
     string c = "c"; 
     Console.WriteLine(c); 
     Console.ForegroundColor = ConsoleColor.Red; 
     string d = "d"; 
     Console.WriteLine(d); 
    } 

輸出將是;

enter image description here

相關問題