正在从控制台读取字符值

本文关键字:字符 读取 控制台 | 更新日期: 2023-09-27 18:24:05

我正在制作一个游戏,需要询问用户他们想要移动的方向,并将其存储为char(L,R,U,D)。

字符将传递给以下方法:

       static void Movement(int n, int rolled, char direction)
        {
        Console.WriteLine("Making a move for " + players[n].Name);

        if (direction == 'u' || direction == 'U')
        {

            if (players[n].Y - rolled < 0)
            {
                players[n].Y = players[n].Y + 8 - rolled;
            }
            else
                players[n].Y = players[n].Y - rolled;
        }
        else if (direction == 'd' || direction == 'D')
        {

            if (players[n].Y + rolled > 7)
            {
                players[n].Y = players[n].Y - 8 + rolled;
            }
            else
                players[n].Y = players[n].Y + rolled;
        }
        else if (direction == 'l' || direction == 'L')
        {

            if (players[n].X - rolled < 0)
            {
                players[n].X = players[n].X + 8 - rolled;
            }
            else
                players[n].X = players[n].X - rolled;
        }
        else if (direction == 'r' || direction == 'R')
        {

            if (players[n].X + rolled > 7)
            {
                players[n].X = players[n].X - 8 + rolled;
            }
            else
                players[n].X = players[n].X + rolled;
        }
            Console.WriteLine(" Please pick a direction: (U,D,L,R");
                char direction = Console.ReadLine();//this gives me an error

Console.ReadLine()给了我一个错误,因为它返回了一个字符串。如何将中的值读取为char并将其存储在direction中?

正在从控制台读取字符值

您正在寻找Console.ReadKey.

char direction = Console.ReadKey().KeyChar;

或者,如果您不想显示密钥,可以使用intercept参数:

char direction = Console.ReadKey(true).KeyChar;

与返回字符串的ReadLine不同,ReadKey返回ConsoleKeyInfo。如果你只需要这个字符,你可以像我上面做的那样从KeyChar中获得它,或者你可以获得密钥代码(或其他类似修饰符的东西)。


此外,您还可以通过使用ToUpper()switch语句来提高代码的可读性:

static void Movement(int n, int rolled, char direction)
{
    direction = char.ToUpper(direction);
    Console.WriteLine("Making a move for " + players[n].Name);
    switch (direction)
    {
        case 'U':
            if (players[n].Y - rolled < 0)
            {
                players[n].Y = players[n].Y + 8 - rolled;
            }
            else
                players[n].Y = players[n].Y - rolled;
            break;
        case 'D':
            if (players[n].Y + rolled > 7)
            {
                players[n].Y = players[n].Y - 8 + rolled;
            }
            else
                players[n].Y = players[n].Y + rolled;
            break;
        case 'L':
            if (players[n].X - rolled < 0)
            {
                players[n].X = players[n].X + 8 - rolled;
            }
            else
                players[n].X = players[n].X - rolled;
            break;
        case 'R':
            if (players[n].X + rolled > 7)
            {
                players[n].X = players[n].X - 8 + rolled;
            }
            else
                players[n].X = players[n].X + rolled;
            break;
        default:
            throw new ArgumentException("Unknown direction " + direction);
    }
}

使用Console.ReadKey()而不是Console.ReadLine()。ReadLine方法将以字符串的形式读取整行,但ReadKey方法将读取第一个字符。但它返回一个ConsoleKeyInfo对象。使用ConsoleKeyInfo.KeyChar获取字符
但我建议您使用枚举而不是字符,因为如果您决定更改方向表示,使用枚举会更容易。

您考虑过Console.ReadKey方法吗?这可以读取一个可以从控制台处理的字符。

使用Console.ReadKey()存储字符值。例如,

direction=Console.ReadKey();