scanf()必须和fflush()一起用才能不出现论坛最近经常提的错误
最近大家总是遇到输入问题,就是用scanf("%d",&n)的时候输入的值经常报错,就是逻辑上是没有问题得,但是结果一直错.其实是涉及一个FFlush( )函数问题.下面是一段关于java上关于FFlush( )的类似的解释.Topic: 谁能解释一下System.out.flush()的作用
Print this page
--------------------------------------------------------------------------------
1.谁能解释一下System.out.flush()的作用 Copy to clipboard
Posted by: zwj0231075
Posted on: 2006-01-02 10:51
谁能帮我解释一下System.out.flush()的作用,可以给出一个简单的实例吗??
谢谢!!
--------------------------------------------------------------------------------
2.Re:谁能解释一下System.out.flush()的作用 [Re: zwj0231075] Copy to clipboard
Posted by: why
Posted on: 2006-01-02 11:05
For this kind of question, please check the Java API:
[url]http://java.[/url]()
public void flush()
Flush the stream. This is done by writing any buffered output bytes to the underlying output stream and then flushing that stream.
If you spend a little time to google for System.out.flush(), you should be able to find something similar to the following:
When System.out.print() or System.out.println() is performed, the printed text doesn't go straight to the screen. Instead, the text gets sent to a special memory buffer, from which it will eventually be written to the screen. However, sometimes events can occur that will stop the buffer from being written to the screen (another process may write to the screen, for example). System.out.flush() forces the buffer to be immediately written to the screen
[url]https://classes.yale.edu/cpsc112a/help/faq.html#flush[/url]
个人理解就是,用scanf得时候是送到缓冲区了,没有送到内存里面,应该用flush()把缓冲的数据送到内存里面.