C# ReadLine()方法不支持';我不接受这条线。有什么解决办法吗?
ReadLine()方法不接受字符串,程序在执行时没有读取输出中显示的字符串。C# ReadLine()方法不支持';我不接受这条线。有什么解决办法吗?,c#,console.readline,C#,Console.readline,ReadLine()方法不接受字符串,程序在执行时没有读取输出中显示的字符串。 using System; namespace ReadReadLineMethod { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); //using read method
using System;
namespace ReadReadLineMethod
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//using read method
Console.WriteLine("Write a character: ");
char a=(char)Console.Read();
Console.WriteLine(a);
//using readline method
Console.WriteLine("Enter a line: ");
string s=Console.ReadLine();
Console.WriteLine(s);
}
}
}
您的问题是由Read调用引起的,该调用只从标准输入流中读取一个字符,但当您键入字母“a”后接Enter键时,您正在输入3个字符:字母“a”、回车符和换行符。最后两个字符不会通过读取从输入流中删除,并且在调用ReadLine时仍然存在。当然,这会导致ReadLine立即退出而不返回任何内容 您可以将Read调用更改为另一个ReadLine,以删除由enter键插入的CR/LF,如果需要单个字符,可以从返回的字符串中提取它
Console.WriteLine("Write a character: ");
string input = Console.ReadLine();
// Decide what default you want
char a = input.Length > 0 ? input[0] : ' ';
Console.WriteLine(a);
控制台输入是行缓冲的,您必须按Enter键提交输入。考虑Read Ky(),因此,您的建议类似于<代码> Strase=控制台。