强悍の绝巴哈

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  • 控制台打印一行
    Console.WriteLine():打印指定内容后自动换行。
  • 控制台打印
    Console.Write():(在光标位置)打印指定内容。
  • 等待键盘输入
    Console.ReadKey():使窗口停留一下,直到点击键盘任一键为止。
  • 等待键盘输入并且提取输入的内容

  char input = Console.ReadKey(true).keychar;
    switch(input)
    {
      case 'a':
        Console.WriteLine("a");
        break;
      default:
        break;
    }


    ConsoleKey key = Console.ReadKey(true).key;
    switch(key)
    {
      case ConsoleKey.W:
        Console.WriteLine("W");
        break;
      default:
        break;
    }

  • 读取输入
    Console.ReadLine():读取一行的输入内容,返回string类型。
    Console.Read():读取一个字符的输入,返回int类型。
  • 隐藏/显示控制台光标
    隐藏:Console.CursorVisible = false;
    显示:Console.CursorVisible = true;
  • 调整鼠标在控制台中的位置
    Console.SetCursorPosition(x,y);
    using System;

    namespace Chess
    {

        struct Vector2
        {
            public int x;
            public int y;
        }

        struct Cube
        {
            public Vector2 size;
            public Vector2 pos;
        }

        class Program
        {
            static void Main(string[] args)
            {
                Cube cube = new Cube();

                cube.size.x = 5;
                cube.size.y = 5;
                cube.pos.x = 5;
                cube.pos.y = 5;

                for (int i = 0; i < cube.size.x; i++)
                {
                    for (int j = 0; j < cube.size.y; j++)
                    {
                        Console.SetCursorPosition((cube.pos.x + i) * 2, cube.pos.y + j);
                        Console.Write("■");
                    }
                }

            }
        }
    }


控制台中y方向每行有两个单位,因此(x,y)当x = 2y时对应一个正方形的点。

  • 清除控制台的输出信息
    Console.Clear();
posted on 2022-06-20 05:14  强悍の绝巴哈  阅读(378)  评论(0编辑  收藏  举报