【发布时间】:2017-02-24 10:29:32
【问题描述】:
我搜索了很多,但没有找到准确的答案。
为什么我的程序在读出我的回车后会多出一行(所以是两个空行)?
当我在carriageReturn (13) 之前完成while 循环时,它会在“c”之后直接打印“------”。
这是我的程序:
import java.io.*;
class IOIntro {
public static void main(String args[]) throws IOException {
int letter = 0;
System.out.print("Type a letter and press Enter: ");
while((letter = System.in.read ()) !=10) { //loops throw whole inputStream until there is a new Line Feed
System.out.println("You typed: " + letter);
System.out.println((char) letter);
}
System.out.print("--------");
}
}
13(回车)之后,10(换行)之前的输出:
Type a letter and press Enter: ads
You typed: 97
a
You typed: 100
d
You typed: 115
s
You typed: 13
--------
13 前的输出(回车):
Type a letter and press Enter: ads
You typed: 97
a
You typed: 100
d
You typed: 115
s
--------
感谢您的帮助。
【问题讨论】:
-
因为您正在打印换行符,所以
println以换行符结束该行。 -
谢谢!明白了!
-
请注意,这只发生在 Windows 或其他使用
\r\n作为换行符分隔符的平台上。检查this demo,它演示了\n- 和\r\n-style 换行符。
标签: java inputstream