【问题标题】: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在 C# 中,我接受一个字符输入,然后输出一些东西。但是在输入控制后不等待输入并直接给出输出
【发布时间】:2017-12-19 19:39:41
【问题描述】:

我有以下代码:

使用系统;

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'

【问题讨论】:

  • 虽然不是完全重复,但这篇文章可能会对您有所帮助:stackoverflow.com/questions/19860677/…
  • 究竟为什么需要拥有并进入?
  • 没什么,只是有一些额外的 C# 技能来做一些机动:)

标签: c# char console.readline console.readkey


【解决方案1】:

例如尝试解析:

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

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

【讨论】:

  • 谢谢,它有效。你能解释一下“char.TryParse(Console.ReadLine(), out ch);”的意思吗?我是 C# 新手。
  • @puregeek 方法Console.ReadLine() 返回一个string,它就像一个由许多字符组成的整个文本。您可以直接使用它,如string st = Console.WriteLine();,但随后您将修改程序的其余部分。相反,我使用了TryParse 方法来检查是否可以“看到”string 作为char。当它成功时,它会将其结果放入out 参数中。当它失败时,它不会爆炸,它只是将零字符 '\0' 放入那个 out 变量中。
  • 这意味着,例如,如果我输入“ab”而不是“a”,然后按 Enter,那么 '\0' 会存储在 out 中? @JeppeStigNielsen
  • @puregeek 是的!我们得到char.TryParse("ab", out ch),它不会解析,你得到'\0'。另一种选择,在顶部添加using System.Linq;,将只是char ch = Console.ReadLine().FirstOrDefault();,它采用字符串的第一个字符,并且仅在字符串没有字符的情况下诉诸'\0'(空字符串,当用户立即按 Enter)。
  • 附带说明,这是一个请求,您能说一下,我们可以将八进制文字分配给字节吗? @JeppeStigNielsen
【解决方案2】:

考虑转动您的代码以接受输入作为参数,并使用 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();
}
}
} 

【讨论】:

  • 但它不会等待输入,也不会从控制台获取输入。从命令行获取参数是另一回事。另外,Convert.ToChar 如果字符串长度不为 1,则会爆炸。
  • 给出错误(5,31):错误CS1552:数组类型说明符,[],必须出现在参数名称和(7,37)之前:错误CS1002:;预计
【解决方案3】:

只需获取 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。

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2015-12-25
      • 1970-01-01
      相关资源
      最近更新 更多