我想在要求输入的同一行上打印出提示,但是当我在intellij中执行此代码时,它仅在输入内容后才打印提示。
我尝试在工作的第一个printf的末尾添加一个'%n',但这并不能满足我的要求,因为我希望输入在同一行上。
我也尝试在第一个printf之后使用System.out.flush(),但这似乎无济于事。
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
Scanner input = new Scanner(System.in);
System.out.printf("Input something: ");
System.out.flush();
String str = input.nextLine();
System.out.printf("You input: %s\n", str);
}
}
我希望将printf(“ Input something:”)打印在我输入的同一行上,就像这样(来自Notepad ++):
但是当我在intellij中运行相同的代码时,这就是我得到的: