在java中打印多行输出而不使用换行符

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

Print multiple lines output in java without using a new line character

java

提问by Rakesh

this is one of the interview question. I am supposed to print multiple lines of output on command line, without using the newline(\n) character in java. I tried googling for this, didn't find appropriate answers. If i am printing 5 numbers, then it should print in the following fashion. But I am not supposed to use the newline character nor loops either. I have to print this using a singleprintln()statement. Can you give me some ideas ? Thanks !

这是面试问题之一。我应该在命令行上打印多行输出,而不使用\njava 中的 newline( ) 字符。我试过谷歌搜索,没有找到合适的答案。如果我打印 5 个数字,那么它应该以下列方式打印。但我也不应该使用换行符或循环。我必须使用单个println()语句打印它。你能给我一些想法吗?谢谢 !

1
2
3
4
5

采纳答案by Zack Marrapese

You can do it recursively:

您可以递归地执行此操作:

public void foo(int currNum) {
  if (currNum > 5) 
    return;
  println(currNum);
  foo(currNum + 1);
}

Then you are only using a single printlnand you aren't using a for or while loop.

然后你只使用一个println,你没有使用 for 或 while 循环。

回答by Thomas

Probably cheating based on the requirements, but technically only 1 println statement and no loops.

根据要求可能作弊,但技术上只有 1 个 println 语句且没有循环。

public int recursivePrint(int number)
{
  if (number >=5 )
    return number;
  else
    System.out.println(recursivePrint(number++));
}

回答by SimonSez

If you're just not allowed of using \nand println()then you can get the systems line.separator, e.g.

如果您只是不被允许使用\nprintln()那么您可以获得系统line.separator,例如

String h = "Hello" + System.getProperty("line.separator") + "World!"

Hope this helped, have Fun!

希望这有帮助,玩得开心!

回答by anubhava

One way is this: Platform Independent

一种方法是:平台独立

final String EOL = System.getProperty("line.separator");
System.out.println('1' + EOL + '2' + EOL + '3' + EOL + '4' + EOL + '5');

This is Platform Dependent

这是平台相关的

char eol = (char) 13;
System.out.println("" + '1' + eol + '2' + eol + '3' + eol + '4');

回答by Mister Smith

Ok, now I think I understand your question. What about this?

好的,现在我想我明白你的问题了。那这个呢?

println(String.format("%d%n%d%n%d%n%d%n%d%n", 1, 2, 3, 4, 5));

回答by Perry Monschau

No loops, 1 println call, +flexibility:

无循环,1 次 println 调用,+灵活性:

public static void main (String[] args) {
    print(5);
}

final String newLine = System.getProperty("line.separator");
public void print(int fin) {
    System.out.println(printRec("",1,fin));
}
private String printRec(String s, int start, int fin) {
    if(start > fin)
        return s;
    s += start + newLine;
    return printRec(s, start+1, fin);
}

回答by wattostudios

There are many ways to achieve this...

有很多方法可以实现这一目标......

One alternative to using '\n' is to output the byte value for the character. So, an example to print out your list of the numbers 1-5 in your example...

使用 '\n' 的一种替代方法是输出字符的字节值。因此,在示例中打印出数字 1-5 列表的示例...

char line = (char)10;
System.out.println("1" + line+ "2" + line+ "3" + line + "4" + line+ "5");

You could also build a byte[] array or char[] array and output that...

您还可以构建一个 byte[] 数组或 char[] 数组并输出...

char line = (char)10;
char[] output = new char[9]{'1',line,'2',line,'3',line,'4',line,'5'};
System.out.println(new String(output));

回答by Chandra Sekhar

The ASCII value of new Line is 10. So use this

new Line 的 ASCII 值是 10。所以用这个

char line = 10;
System.out.print("1" + line + "2" + line ......);

回答by root

ANSI terminal escape codes can do the trick.

ANSI 终端转义码可以解决问题。

Aside:Since System.outis a PrintStream, it may not be able to support the escape codes.

旁白:Since System.out是一个PrintStream,它可能无法支持转义码。

However,you can define your own println(msg)function, and make one call to that. Might be cheating, but unless they explicitly say System.out.println, you're golden (hell, even if they do, you can define your own object named Systemin the local scope using a class defined outside your function, give it a field outwith a function println(msg)and you're still scot-free).

但是,您可以定义自己的println(msg)函数,并对其进行一次调用。可能是作弊,但除非他们明确说System.out.println,你是金子(地狱,即使他们这样做,你也可以System使用在你的函数之外定义的类来定义你自己在本地范围内命名的对象,给它一个out带有函数的字段,println(msg)然后你'仍然是免费的)。