java 回车Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12011431/
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-10-31 07:15:50  来源:igfitidea点击:

Carriage return Java

javacarriage-return

提问by faraday

is there any way to print a line in java and then go back to the beginning of that line, i tried using \r however this only prints a new line and does not go back to the original line.

有没有办法在java中打印一行然后返回到该行的开头,我尝试使用\ r但是这只会打印一个新行而不会返回到原始行。

So basically if the user inputs "Hello this is lol"

所以基本上如果用户输入“你好,这是哈哈”

I want to print all the a's in the sentence (none), all the b's, etc... eg.)" e " then"He " --> this however must be on the same line as above and you must be able to see the change.

我想打印句子中的所有 a(无),所有 b 等......例如)" e " then"He " --> 然而,这必须与上面的同一行,你必须能够看到变化。

Is there any way to do this in java?

有没有办法在java中做到这一点?

回答by Colin D

This is not really a java question, but has more to do with the behavior of your terminal/console.

这不是真正的 Java 问题,而是更多地与您的终端/控制台的行为有关。

You are sending the correct character to return to the beginning of the line '\r' but it sounds like your console is not handling this correctly.

您正在发送正确的字符以返回到 '\r' 行的开头,但听起来您的控制台没有正确处理这个问题。

You should also be using the print()and not println()function (or whatever the methods are called on the specific object you are using to write). The println()function will add a '\n' character which will cause a new line to appear.

您还应该使用print()and notprintln()函数(或在您用来编写的特定对象上调用的任何方法)。该println()函数将添加一个 '\n' 字符,这将导致出现一个新行。

回答by Marko Topolnik

I ran this on my Mac, which is a FreeBSD underneath:

我在我的 Mac 上运行了这个,它是下面的 FreeBSD:

public static void main (String[] args) {
  System.out.println("Hello there\rWho's");
}

It printed out

它打印出来

Who's there

If you are running this on Windows, then I cannot be sure what they'll do, but all *nixes should behave as posted.

如果您在 Windows 上运行它,那么我无法确定它们会做什么,但是所有 *nixes 都应该像发布的那样运行。

回答by Bohemian

It depends what you mean by "print a line". If you're talking about the command line of an OS window, then you're going to nee to about output terminal control characters to control the cursor.

这取决于您所说的“打印一条线”是什么意思。如果您在谈论操作系统窗口的命令行,那么您将需要输出终端控制字符来控制光标。

It depends very much on which operating system you are using, but there are libraries output that can make it simpler.

这在很大程度上取决于您使用的操作系统,但有一些库输出可以使其更简单。

You might be able to output the backspace character \bto erase the current line, but it may not work.

您也许可以输出退格字符\b来擦除当前行,但它可能不起作用。