[英]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.