Java - 是否可以在输入数字的同一行上继续打印文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7022853/
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 - Is it possible to keep printing text on the same line you enter a number on?
提问by
I want to be able to enter a number using nextInt()and then print text on the same line as the number I entered. For example...
我希望能够使用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!");
And the output would look something like this:
输出看起来像这样:
Enter your number: 4356
I want this on the same line as my number!
Is there a way I can get the text to print right after the number? So it would look something like this?...
有没有办法让我在数字后立即打印文本?那么它看起来像这样吗?...
Enter your number: 4356 I want this on the same line as my number!
回答by adarshr
Have you tried
你有没有尝试过
System.out.print("\rI want this on the same line as my number!");
But note that because the user has to press the Enterkey, there's no way (AFAIK) to go a line backwards and erase till the beginning of it.
但请注意,因为用户必须按下该Enter键,所以无法(AFAIK)向后移动一行并擦除直到它的开头。
回答by Luigi Plinge
On Windows, I'm pretty sure the answer is "no", because there doesn't seem to be any reverse line feed (see this question: Reverse line feed in Windows / Java).
在 Windows 上,我很确定答案是“否”,因为似乎没有任何反向换行(请参阅此问题:Windows / Java 中的反向换行)。