关于Console.Read()的一些知识 [英] Something about Console.Read()

查看:85
本文介绍了关于Console.Read()的一些知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数返回一个字符,但是您必须按"enter"(输入).在获取字符之前.这意味着您获得了整整一行,可以通过Console.ReadLine()获得,这让我非常困惑.还有其他获取字符的方法,以便您可以输入单词或 数字,以 空格分隔

This function return a char, but you have to press "enter" before you get the char. It means you get a whole line, you can get by Console.ReadLine(), it confused me so much. Is there any other way to get a char, so that you can enter a word or number seperate by  white space

推荐答案

Read仅读取下一个字符,而ReadLine返回整个字符串.该行为可能看起来相同,但事实并非如此.如果用户输入"abc",然后按ENTER键,则Read返回"a",而ReadLine返回"abc".此外 如果再次调用Read,它将返回"b",而readLine将等待您输入另一行文本,然后按Enter.当输入缓冲区中没有更多数据时,只读块.

Read reads just the next character while ReadLine returns the entire string. The behavior may appear the same but it isn't. If a user types in "abc" and then presses ENTER then Read returns 'a' while ReadLine returns "abc". Furthermore if you call Read again then it'll return 'b' whereas readLine will wait for you to enter another line of text and press ENTER. Read only blocks when there is no more data in the input buffer.

如果您试图获得类似于C ++中cin的行为(这也要求您按Enter将其移至输入缓冲区),则可以在循环中使用Read直到看到空白字符.然后,您将拥有第一个令牌".从 输入.是因为单行上有多个令牌还是用户在单独的行上输入了令牌就无关紧要(与cin一样).

If you're trying to get behavior similar to cin in C++ (which also requires that you press ENTER to move it to the input buffer) then you could use Read in a loop until you see a whitespace character. You then have the first "token" from the input. Whether that occurred because there are multiple tokens on a single line or the user entered them on separate lines is irrelevant (just like with cin).

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于关于Console.Read()的一些知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆