0

我想在要求输入的同一行上打印一个提示,但是当我在 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 中运行相同的代码时,这就是我得到的:

4

0 回答 0