第十课:
控制台小钢琴程序:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请按1到7来操作!");
while (true)
{
bool mark = false;
ConsoleKeyInfo s = Console.ReadKey(true);
//ReadKey直接输入不用回车。true代表按完键后,不在控制台上显示!
switch (s.KeyChar)
{
case '1': Console.Beep(1000, 500);
break;
case '2': Console.Beep(2000, 500);
break;
case '3': Console.Beep(3000, 500);
break;
case '4': Console.Beep(3500, 500);
break;
case '5': Console.Beep(4000, 500);
break;
case '6': Console.Beep(4500, 500);
break;
case '7': Console.Beep(5000, 500);
break;
default:
{
mark = true;
Console.WriteLine("输入错误!请输入1到7的数字!");
}
break;
}
if (!mark)
{
//把控制台背景枚举色转换成枚举。
Console.BackgroundColor = (ConsoleColor)int.Parse(s.KeyChar.ToString());
for (int i = 1; i <= int.Parse(s.KeyChar.ToString()); i++)
{
Console.Write(" ");
}
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(s.KeyChar);
}
}
}
}