Console.Read() 和 Console.ReadLine() 之间的区别?
我是这个领域的新手,我很困惑:Console.Read() 和 Console.ReadLine() 之间的真正区别是什么?
I'm new to this field and I'm very confused: what is the real difference between Console.Read()
and Console.ReadLine()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Console.Read()
仅从标准输入读取下一个字符,和
Console.ReadLine()
从标准输入流读取下一行字符。控制台应用程序的标准输入是用户在应用程序的控制台 UI 中键入的单词输入的。尝试用Visual studio 创建一下,自己看看。
Console.Read()
reads only the next character from standard input,and
Console.ReadLine()
reads the next line of characters from the standard input stream.Standard input in case of Console Application is input from the user typed words in console UI of your application. Try to create it by Visual studio, and see by yourself.
这些是system.console的方法
另一个突出的例子,按 Y 或 n 继续。
标准输入流。与 ReadKey 类似,不同之处在于它返回一个
整数。它返回输入流中的下一个字符,如果没有更多的字符要读取,则返回 (-1)。
(还有更多的 system.console 方法,如 write() 和 writeline() ,它们用于在命令行中写入,其行为与 read() 和 readline() 方法类似)
这已经清楚地描述了以及 MSDN 文档中的示例(上面包含链接)。
These are the methods of system.console
Another prominent example, Press Y or n to continue.
standard input stream. Similar to ReadKey except that it returns an
integer. It returns the next character from the input stream, or returns (-1) if there is no more character to be read.
(There are more system.console methods like write() and writeline() as well which are used to write in command line, behaving similarly as read() and readline() methods)
This was clearly described with examples in the MSDN documentation (links are included above).
Console.Read()
仅读取单个字符,而Console.ReadLine()
读取所有字符直至行尾。Console.Read()
reads just a single character, whileConsole.ReadLine()
reads all characters until the end of line.关于这一点,MSDN其实说的很清楚了。
MSDN is actually pretty clear on this one.
基本区别是:
粘贴上面的代码并输入“c”,输出将为 99。
也就是说 Console.Read 给出 int 值,但该值将是该值的 ASCII 值..
另一方面..
它给出输入流中给出的字符串。
The basic difference is:
paste above code and give input 'c', and the output will be 99.
That is Console.Read give int value but that value will be the ASCII value of that..
On the other side..
It gives the string as it is given in the input stream.
Console.Read()
读取单个键,其中Console.Readline()
等待 Enter 键。Console.Read()
reads a single key, whereConsole.Readline()
waits for the Enter key.Console.Read()
基本上读取一个字符,因此如果您在控制台上按下一个键,那么控制台将关闭,同时Console.Readline()
将读取整个字符串。Console.Read()
basically reads a character so if you are on a console and you press a key then the console will close, meanwhileConsole.Readline()
will read the whole string.Console.Read()
Console.ReadLine()
Console.ReadKey()
Console.Read()
Console.ReadLine()
Console.ReadKey()
Read()、ReadLine() 和 Readkey() 方法的区别如下:
Read():这是 Console 类中的静态方法:
粘贴上面的代码并输入“1”,然后输出将为 49。即 Console.Read 给出 int 值,但该值将是该值的 ASCII 值。
ReadLine():
它给出输入流中给出的字符串。
ReadKey():此方法用于在按下任意键时保持输出屏幕。
Read()和ReadLine()用于退出的回车键。
The difference of Read(),ReadLine() and Readkey() method are given below:
Read():This is static method in Console class:
paste above code and give input '1', and the output will be 49. That is Console.Read give int value but that value will be the ASCII value of that.
ReadLine():
It gives the string as it is given in the input stream.
ReadKey(): this method is used to hold the output screen.when any key is press.
Read() and ReadLine() is used the enter key for exit.
C# 中 Read()、Readline() 和 ReadKey() 之间的区别:
Read()
- 接受字符串值并返回字符串值。Readline()
- 接受字符串并返回整数。ReadKey()
- 接受字符并返回字符。总结:
Difference between Read(), Readline() and ReadKey() in C#:
Read()
- Accept the string value and return the string value.Readline()
- Accept the string and return Integer.ReadKey()
- Accept the character and return Character.Summary:
Console.Read()
用于从标准输入流读取下一个字符。当我们只想读取单个字符时,请使用Console.Read()。
Console.ReadLine()
用于从标准输入流读取一行字符。当我们想要读取一行字符时,请使用Console.ReadLine()。
Console.Read()
is used to read next charater from the standard input stream.When we want to read only the single character then use
Console.Read()
.Console.ReadLine()
is used to read aline of characters from the standard input stream.when we want to read a line of characters use
Console.ReadLine()
.=>只从标准输入读取一个字符
=>从标准输入读取该行中的所有字符
=> reads only one character from the standard input
=> reads all characters in the line from the standard input