Console.Read() 和 Console.ReadLine() 之间的区别?

发布于 2024-11-26 07:59:09 字数 69 浏览 1 评论 0原文

我是这个领域的新手,我很困惑: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

假装不在乎 2024-12-03 07:59:09

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.

耳钉梦 2024-12-03 07:59:09

这些是system.console的方法

  • ReadKey()(返回一个字符):从标准输入流中仅读取一个字符或命令 线。通常在您在控制台中向用户提供可供选择的选项时使用,例如选择 A、B 或 C
    另一个突出的例子,按 Y 或 n 继续
  • 阅读行()(返回字符串):或 Console.Readline() 从标准输入流或命令行读取一行。例如,它可用于要求用户输入他们的姓名或年龄。它会读取所有字符,直到我们按输入
  • 阅读() (返回一个int):或者 Console.Read() 只从
    标准输入流。与 ReadKey 类似,不同之处在于它返回一个
    整数。它返回输入流中的下一个字符,如果没有更多的字符要读取,则返回 (-1)。

(还有更多的 system.console 方法,如 write() 和 writeline() ,它们用于在命令行中写入,其行为与 read() 和 readline() 方法类似)

这已经清楚地描述了以及 MSDN 文档中的示例(上面包含链接)。

These are the methods of system.console

  • ReadKey() (returns a character): reads only one single character from the standard input stream or command line. Usually used when you're giving options to the user in the console to select from, such as select A, B or C.
    Another prominent example, Press Y or n to continue.
  • ReadLine() (returns a string): or Console.Readline() reads a single line from the standard input stream or the command line. As an example, it can be used to ask the user enter their name or age. It reads all the character till we press enter.
  • Read() (returns an int): or Console.Read() reads only one single character from the
    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).

情绪操控生活 2024-12-03 07:59:09

Console.Read() 仅读取单个字符,而 Console.ReadLine() 读取所有字符直至行尾。

Console.Read() reads just a single character, while Console.ReadLine() reads all characters until the end of line.

演出会有结束 2024-12-03 07:59:09

关于这一点,MSDN其实说的很清楚了。

MSDN is actually pretty clear on this one.

  • Console.Read: Reads the next character from the standard input stream.
  • Console.ReadLine: Reads the next line of characters from the standard input stream.
凝望流年 2024-12-03 07:59:09

基本区别是:

int i = Console.Read();
Console.WriteLine(i);

粘贴上面的代码并输入“c”,输出将为 99。
也就是说 Console.Read 给出 int 值,但该值将是该值的 ASCII 值..

另一方面..

string s = Console.ReadLine();
Console.WriteLine(s);

它给出输入流中给出的字符串。

The basic difference is:

int i = Console.Read();
Console.WriteLine(i);

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..

string s = Console.ReadLine();
Console.WriteLine(s);

It gives the string as it is given in the input stream.

↘紸啶 2024-12-03 07:59:09

Console.Read() 读取单个键,其中 Console.Readline() 等待 Enter 键。

Console.Read() reads a single key, where Console.Readline() waits for the Enter key.

十雾 2024-12-03 07:59:09

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, meanwhile Console.Readline() will read the whole string.

又爬满兰若 2024-12-03 07:59:09

Console.Read()

  • 它只接受用户输入的单个字符并返回其 ASCII 代码。
  • 数据类型应该是int。因为它返回一个 ASCII 整数值。
  • 前-> int value = Console.Read();

Console.ReadLine()

  • 它从用户输入中读取所有字符。 (并按回车键完成)。
  • 它返回一个字符串,因此数据类型应该是字符串。
  • 前-> string value = Console.ReadLine();

Console.ReadKey()

  • 它读取用户按下的键并返回其名称。输入前不需要按 Enter 键。
  • 它是一个结构数据类型,即 ConsoleKeyInfo。
  • 前-> ConsoleKeyInfo key = Console.ReadKey();

Console.Read()

  • It only accepts a single character from user input and returns its ASCII Code.
  • The data type should be int. because it returns an integer value as ASCII.
  • ex -> int value = Console.Read();

Console.ReadLine()

  • It read all the characters from user input. (and finish when press enter).
  • It returns a String so data type should be String.
  • ex -> string value = Console.ReadLine();

Console.ReadKey()

  • It read that which key is pressed by the user and returns its name. it does not require to press enter key before entering.
  • It is a Struct Data type which is ConsoleKeyInfo.
  • ex -> ConsoleKeyInfo key = Console.ReadKey();
千仐 2024-12-03 07:59:09

Read()、ReadLine() 和 Readkey() 方法的区别如下:

Read():这是 Console 类中的静态方法:

int i = Console.Read();//it always return int value.
Console.WriteLine(i);

粘贴上面的代码并输入“1”,然后输出将为 49。即 Console.Read 给出 int 值,但该值将是该值的 ASCII 值。

ReadLine():

string s= Console.ReadLine();//it always return string value.
Console.WriteLine(s);

它给出输入流中给出的字符串。

ReadKey():此方法用于在按下任意键时保持输出屏幕。
Read()和ReadLine()用于退出的回车键。

The difference of Read(),ReadLine() and Readkey() method are given below:

Read():This is static method in Console class:

int i = Console.Read();//it always return int value.
Console.WriteLine(i);

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():

string s= Console.ReadLine();//it always return string value.
Console.WriteLine(s);

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.

情话墙 2024-12-03 07:59:09

C# 中 Read()、Readline() 和 ReadKey() 之间的区别:

  • Read() - 接受字符串值并返回字符串值。
  • Readline() - 接受字符串并返回整数。
  • ReadKey() - 接受字符并返回字符。

总结:

  1. 这三个方法主要用在Console应用程序中,用于返回不同的值。
  2. 如果我们使用 Read line 或 Read(),我们需要按 Enter 键返回代码。
  3. 如果我们使用 Read key(),我们可以按任意键返回应用程序中的代码。

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:

  1. These three methods are mainly used in Console application and these are used for returning different values.
  2. If we use Read line or Read(), we need press Enter to come back to code.
  3. If we using Read key(), we can press any key to come back code in application.
欢你一世 2024-12-03 07:59:09

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().

氛圍 2024-12-03 07:59:09
Console.Read()

=>只从标准输入读取一个字符

Console.ReadLine()

=>从标准输入读取该行中的所有字符

Console.Read()

=> reads only one character from the standard input

Console.ReadLine()

=> reads all characters in the line from the standard input

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文