Java-是否可以在输入数字的同一行上继续打印文本?

Java-是否可以在输入数字的同一行上继续打印文本?,java,newline,Java,Newline,我希望能够使用nextInt()输入一个数字,然后在与我输入的数字相同的行上打印文本。例如 Scanner scan = new Scanner(System.in); System.out.print("Enter your number: "); int x = scan.nextInt(); //After you press 'enter' it goes to the next line and keeps printing text System.out.print("I want

我希望能够使用nextInt()输入一个数字,然后在与我输入的数字相同的行上打印文本。例如

Scanner scan = new Scanner(System.in);
System.out.print("Enter your number: ");
int x = scan.nextInt(); //After you press 'enter' it goes to the next line and keeps printing text
System.out.print("I want this on the same line as my number!");
输出结果如下所示:

Enter your number: 4356
I want this on the same line as my number!
有没有办法让文字在数字之后打印出来?所以看起来是这样的

Enter your number: 4356 I want this on the same line as my number!
你试过了吗

System.out.print("\rI want this on the same line as my number!");

但请注意,由于用户必须按Enter键,因此无法(AFAIK)向后移动一行并擦除它的开头。

在Windows上,我很确定答案是“否”,因为似乎没有任何反向换行(请参见此问题:)。

虽然我无法给您答案,我怀疑这可能是一个控制台问题,而不是Java本身的问题。当您在输入数字后点击
Enter
键时,是控制台决定将光标移动到新行的开头-不是Java决定这样做。因此,任何解决方案都会涉及Java以某种方式改变控制台的自然行为,并且可能是特定于环境的。这不会打印前一行上的数字,而是打印结果,然后在前一行和结果行之间插入一行。还在下一行打印,而不是在同一行。@Logan-奇怪!这对我来说非常有效!只需将这三行放在main中,并在命令提示符下运行它:System.out.print(“helloworld:”);睡眠(5000);System.out.print(“\rHey,我是……”)@Logan-问题是当我们从用户那里读取输入时。用户必须按“回车”键才能进行此操作。我们不能把一条线倒回去,把回车键移走。