0

我有以下代码:

使用系统;

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'
4

4 回答 4

1

例如尝试解析:

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

'\0'如果用户在 Enter 之前键入了太多字符或没有字符,您将得到空字符。或者,您可以获取 的返回值TryParse,一个布尔值,告诉解析是否成功。

于 2017-12-19T19:44:46.193 回答
0

考虑转动您的代码以接受输入作为参数,并使用 System 类 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();
}
}
} 
于 2017-12-19T19:44:40.717 回答
0

只需抓住 Console.ReadLine() 返回的字符串的第一个字符

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。

于 2020-01-08T18:56:21.457 回答
0

对于更脏但非常灵活的代码,您可以使用 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' 中调用函数)。

于 2020-11-21T17:29:09.497 回答