第十講:控制檯鋼琴
例:namespace Chapter10Demo1
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("請按1至7來操做!");
while (true)
{
bool mark = false;//用來設置非1至7按鍵標誌
ConsoleKeyInfo cki = Console.ReadKey(true);//true表示按完鍵後鍵不在控制檯上顯示
//Console.WriteLine(cki.KeyChar);
switch (cki.KeyChar)
{
case '1':
Console.Beep(800, 500);
ide
break;
case '2':
Console.Beep(1000, 500);
spa
break;
case '3':
Console.Beep(1200, 500);
string
break;
case '4':
Console.Beep(1400, 500);
it
break;
case '5':
Console.Beep(1600, 500);
class
break;
case '6':
Console.Beep(1800, 500);
static
break;
case '7':
Console.Beep(2000, 500);di
break;
default:
{
mark = true;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("請輸入1至7之間的數!");
Console.ForegroundColor = ConsoleColor.White;
break;
}view
}vi
if (!mark)
{
//把控制檯背景枚舉色標誌轉換成枚舉
Console.BackgroundColor = (ConsoleColor)int.Parse(cki.KeyChar.ToString());
for (int i = 1; i < int.Parse(cki.KeyChar.ToString()) * 3; i++)
{
Console.Write(" ");
}
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(cki.KeyChar.ToString());
}while
}
} } }