java Java如何在同一行上获取scanner.nextLine()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36385221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java How to get scanner.nextLine() on the same line?
提问by Dan13_
I have a program that takes input from the user from the command line. Annoyingly the first time I use the scanner the inputted text goes on the next line (after the System.out.println
). However, all the other times it is on the same line as the System.out.println
.
我有一个从命令行获取用户输入的程序。令人讨厌的是,当我第一次使用扫描仪时,输入的文本出现在下一行(在 之后System.out.println
)。但是,所有其他时间它都与System.out.println
.
Code:
代码:
//Create a scanner
Scanner input = new Scanner(System.in);
//Gets type
System.out.println("--Television Show or Film (t or f): ");
String type = input.nextLine();
input.close();
Output:
输出:
--Television Show or Film (t or f):
t
Desierd Output:
期望输出:
--Television Show or Film (t or f): t
Any help is appreciated. Also, I'm using Eclipse Console.
任何帮助表示赞赏。另外,我正在使用 Eclipse 控制台。
采纳答案by ryekayo
Try:
尝试:
System.out.print("--Television Show or Film (t or f): ");
String type = input.next();
回答by nicomp
Use System.out.print
rather than System.out.println
?
使用System.out.print
而不是System.out.println
?
回答by x80486
The reason is you are using the println
version of System.out
. Use only print
: System.out.print("--Television Show or Film (t or f): ");
原因是您使用的println
是System.out
. 仅使用print
:System.out.print("--Television Show or Film (t or f): ");