在 C# 中,我接受一个字符输入,然后输出一些东西。但是在输入控制之后不等待输入并直接给出输出

In C# I am taking a char input and then I output something. But after input control is not waiting for enter and straightaway giving output

我有以下代码:

使用系统;

namespace MyApp {
class KeyBoardInputTest {
    static void Main() {
        Console.Write("Enter a character >\t");
        char ch = (char)Console.Read();
        Console.WriteLine("");

        if(Char.IsLetter(ch)) {
            if(Char.IsUpper(ch))
                Console.WriteLine("You have entered an upper case character");
            else
                Console.WriteLine("You have entered a lower case character");
        }
        else if(Char.IsNumber(ch))
            Console.WriteLine("You have entered a numeric character");
        else
            Console.WriteLine("You have entered a non alpha-numeric character");
        Console.Read();
    }
}
} 

输入 a 后,它会直接显示结果,无需等待我按 Enter

使用Console.ReadLine()给出错误-

error CS0030: Cannot convert type 'string' to 'char'

使用Console.ReadKey()给出错误-

error CS0030: Cannot convert type 'System.ConsoleKeyInfo' to 'char'

考虑调整您的代码以接受输入作为参数并使用系统 class Convert.ToChar()

将输入转换为字符
using System;

namespace MyApp {

class KeyBoardInputTest {
static void Main(string [] args) {    // This line has been changed
    Console.Write("Enter a character >\t");
    char ch = Convert.ToChar(args[0])   // This line has been changed
    Console.WriteLine("");

    if(Char.IsLetter(ch)) {
        if(Char.IsUpper(ch))
            Console.WriteLine("You have entered an upper case character");
        else
            Console.WriteLine("You have entered a lower case character");
    }
    else if(Char.IsNumber(ch))
        Console.WriteLine("You have entered a numeric character");
    else
        Console.WriteLine("You have entered a non alpha-numeric character");
    Console.Read();
}
}
} 

例如尝试解析:

Console.Write("Enter a character >\t");
char ch;
char.TryParse(Console.ReadLine(), out ch);
Console.WriteLine();

如果用户在 Enter 之前键入太多字符或没有键入任何字符,您将得到空字符 '[=11=]'。或者,您可以选择 TryParse 的 return 值,这是一个布尔值,表示解析是否成功。

只需获取 Console.ReadLine() returns

字符串的第一个字符
namespace MyApp {
class KeyBoardInputTest {
    static void Main() {
        Console.Write("Enter a character >\t");
        char ch = Console.ReadLine()[0];
        Console.WriteLine("");

        if(Char.IsLetter(ch)) {
            if(Char.IsUpper(ch))
                Console.WriteLine("You have entered an upper case character");
            else
                Console.WriteLine("You have entered a lower case character");
        }
        else if(Char.IsNumber(ch))
            Console.WriteLine("You have entered a numeric character");
        else
            Console.WriteLine("You have entered a non alpha-numeric character");
        Console.Read();
    }
}
} 

请记住,这会删除用户可能输入的任何其他字符。如果用户没有输入任何信息,它也会导致 IndexOutOfRangeException。

对于较脏但非常灵活的代码,您可以使用 Try-Catch 来获取输入。

确保使用如下内容初始化变量: ch = 'c';

然后使用这段代码:

    try()          //You can get the try-catch code snippet by typing 'try' and hit enter or tab
    {
       ch = Convert.ToChar(Console.ReadLine());
    }
    catch(Exception e)
    {
       Console.WriteLine("Enter a Valid Character");  //You can also recurse your function after this
    }

这种方法有点不干净,但与其他方法相比,你仍然可以做更多的事情。 (就像在 'catch' 或 'try' 中调用一个函数)。