简体   繁体   English

在 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

I have the following code:我有以下代码:

using System;使用系统;

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();
    }
}
} 

After taking an input such as a it is straightaway showing result without waiting for me to press Enter在输入诸如a之类的输入后,无需等待我按Enter即可直接显示结果

Using Console.ReadLine() gives the error -使用Console.ReadLine()给出错误 -

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

Using Console.ReadKey() gives the error -使用Console.ReadKey()给出错误 -

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

For example try-parse:例如尝试解析:

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

You will get the null character '\\0' if the user types too many characters, or no characters, before Enter.如果用户在 Enter 之前键入太多字符或没有键入字符,您将得到空字符'\\0' Alternatively, you can pick up the return value of TryParse , a boolean telling whether the parsing succeeded.或者,您可以获取TryParse的返回值,这是一个指示解析是否成功的布尔值。

Consider pivoting your code to accept input as arguments and convert the input to a char using the System class Convert.ToChar()考虑旋转代码以接受输入作为参数并使用 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();
}
}
} 

Just grab the first character of the String which Console.ReadLine() returns只需获取 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();
    }
}
} 

Keep in mind this removes any additional characters the user may have entered.请记住,这会删除用户可能输入的任何其他字符。 It will also cause an IndexOutOfRangeException if you the user does not enter any information.如果用户没有输入任何信息,它也会导致 IndexOutOfRangeException。

For a Dirtier, but very flexible code, you can use Try-Catch to get the input.对于较脏但非常灵活的代码,您可以使用 Try-Catch 来获取输入。

Make sure that you initialize the variable with something like: ch = 'c';确保使用以下内容初始化变量: ch = 'c';

Then use this piece of code:然后使用这段代码:

    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
    }

This method is a bit Unclean, but you still can do a lot more in it as compared to other ways.这种方法有点不干净,但与其他方法相比,您仍然可以在其中做更多的事情。 (Like calling a function inside 'catch' or 'try'). (就像在“catch”或“try”中调用函数一样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一RichTextBox控件中的C#输入和输出 - C# Input and Output in the same RichTextBox Control 我想知道如何在C#中执行输入和输出操作 - I want to know how to perform input and output operations in C# 如何在C#中以语音字母输入名称并将其输出 - how can I input a name and output it in phonetic alphabet in C# C#我想在输出中将输入更改为新语言 - C# I want change input to new language in output 如何使用C#绑定标准输入和输出? - How do I tie into the standard input and output with C#? 我正在尝试输入 200 个 X、Y 坐标的列表,然后输入 output 一个包含这些坐标的命令列表 C# - I am trying to input a list of 200 X,Y coordinates and then output a list of commands with those coordinates in them C# 使用C#输入附加输出 - Output append with input in C# 为什么在输入某些内容后立即显示输出 - Why does the output appear as soon as I input something 我从C#调用具有2个参数(输入和输出)的存储过程,但未显示输出参数的值 - I call a Stored procedure with 2 parameters (input and output) from C# but value of output parameter does not show C# 控制台:在 char 变量中输入变量后,如何在 LastName 中输入值? - C# Console : How can i enter a value in LastName after i Enter a variable in the char variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM