Console类的常用方法

偏执的太偏执、 2022-07-15 05:18 219阅读 0赞

int Console.Read() 返回键入字符对应的ascii码

String Console.ReadLine(); 返回键入的字符

ConsoleKeyInfo Console.ReadKey();

  1. public static void Main()
  2. {
  3. ConsoleKeyInfo cki;
  4. // Prevent example from ending if CTL+C is pressed.
  5. Console.TreatControlCAsInput = true;
  6. Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
  7. Console.WriteLine("Press the Escape (Esc) key to quit: \n");
  8. do
  9. {
  10. cki = Console.ReadKey();
  11. Console.Write(" --- You pressed ");
  12. if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
  13. if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
  14. if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
  15. Console.WriteLine(cki.Key.ToString());
  16. } while (cki.Key != ConsoleKey.Escape);
  17. }
  18. // This example displays output similar to the following:
  19. // Press any combination of CTL, ALT, and SHIFT, and a console key.
  20. // Press the Escape (Esc) key to quit:
  21. //
  22. // a --- You pressed A
  23. // k --- You pressed ALT+K
  24. // ► --- You pressed CTL+P
  25. // --- You pressed RightArrow
  26. // R --- You pressed SHIFT+R
  27. // --- You pressed CTL+I
  28. // j --- You pressed ALT+J
  29. // O --- You pressed SHIFT+O
  30. // § --- You pressed CTL+U

Console.Readkey(Boolean)

此方法参数为true时,按键值不会显示在console上。

  1. public static void Main()
  2. {
  3. ConsoleKeyInfo cki;
  4. // Prevent example from ending if CTL+C is pressed.
  5. Console.TreatControlCAsInput = true;
  6. Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
  7. Console.WriteLine("Press the Escape (Esc) key to quit: \n");
  8. do {
  9. cki = Console.ReadKey(true);
  10. Console.Write("You pressed ");
  11. if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
  12. if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
  13. if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
  14. Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
  15. } while (cki.Key != ConsoleKey.Escape);
  16. }
  17. 注意ReadKey()标红色的部分与ReadKey(true)的区别
  18. // This example displays output similar to the following:
  19. // Press any combination of CTL, ALT, and SHIFT, and a console key.
  20. // Press the Escape (Esc) key to quit:
  21. //
  22. // You pressed CTL+A (character '☺')
  23. // You pressed C (character 'c')
  24. // You pressed CTL+C (character '♥')
  25. // You pressed K (character 'k')
  26. // You pressed ALT+I (character 'i')
  27. // You pressed ALT+U (character 'u')
  28. // You pressed ALT+SHIFT+H (character 'H')
  29. // You pressed Escape (character '←')

不显示键下的值,那么这个方法的主要用途就是通过判断用户的输入来终止程序或者在APP中打开/关闭一个window

void Consloe.Write()

void Console.WriteLine();

有各种各样的重载,关于各个类的,各种类型的,转化成字符串打印出来。

值得一说的是还可以按照指定的格式进行打印

  1. // Format a negative integer or floating-point number in various ways.
  2. Console.WriteLine("Standard Numeric Format Specifiers");
  3. Console.WriteLine(
  4. "(C) Currency: . . . . . . . . {0:C}\n" +
  5. "(D) Decimal:. . . . . . . . . {0:D}\n" +
  6. "(E) Scientific: . . . . . . . {1:E}\n" +
  7. "(F) Fixed point:. . . . . . . {1:F}\n" +
  8. "(G) General:. . . . . . . . . {0:G}\n" +
  9. " (default):. . . . . . . . {0} (default = 'G')\n" +
  10. "(N) Number: . . . . . . . . . {0:N}\n" +
  11. "(P) Percent:. . . . . . . . . {1:P}\n" +
  12. "(R) Round-trip: . . . . . . . {1:R}\n" +
  13. "(X) Hexadecimal:. . . . . . . {0:X}\n",
  14. -123, -123.45f);
  15. // Format the current date in various ways.
  16. Console.WriteLine("Standard DateTime Format Specifiers");
  17. Console.WriteLine(
  18. "(d) Short date: . . . . . . . {0:d}\n" +
  19. "(D) Long date:. . . . . . . . {0:D}\n" +
  20. "(t) Short time: . . . . . . . {0:t}\n" +
  21. "(T) Long time:. . . . . . . . {0:T}\n" +
  22. "(f) Full date/short time: . . {0:f}\n" +
  23. "(F) Full date/long time:. . . {0:F}\n" +
  24. "(g) General date/short time:. {0:g}\n" +
  25. "(G) General date/long time: . {0:G}\n" +
  26. " (default):. . . . . . . . {0} (default = 'G')\n" +
  27. "(M) Month:. . . . . . . . . . {0:M}\n" +
  28. "(R) RFC1123:. . . . . . . . . {0:R}\n" +
  29. "(s) Sortable: . . . . . . . . {0:s}\n" +
  30. "(u) Universal sortable: . . . {0:u} (invariant)\n" +
  31. "(U) Universal full date/time: {0:U}\n" +
  32. "(Y) Year: . . . . . . . . . . {0:Y}\n",
  33. thisDate);

{占位符,长度:格式类型}

发表评论

表情:
评论列表 (有 0 条评论,219人围观)

还没有评论,来说两句吧...

相关阅读