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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:21:52  来源:igfitidea点击:

Java How to get scanner.nextLine() on the same line?

java

提问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.printrather than System.out.println?

使用System.out.print而不是System.out.println?

回答by x80486

The reason is you are using the printlnversion of System.out. Use only print: System.out.print("--Television Show or Film (t or f): ");

原因是您使用的printlnSystem.out. 仅使用printSystem.out.print("--Television Show or Film (t or f): ");